Skip to content

Commit

Permalink
Add more notes in Data Structures and Algorithms!
Browse files Browse the repository at this point in the history
  • Loading branch information
kekeandzeyu committed Aug 6, 2024
1 parent ac34c3e commit 00665e6
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Writerside/topics/C-Programming.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<show-structure for="chapter" depth="3"/>
<show-structure for="chapter" depth="3"></show-structure>

# C++ Programming

Expand Down
68 changes: 68 additions & 0 deletions Writerside/topics/Data-Structures-and-Algorithms-2.md
Original file line number Diff line number Diff line change
Expand Up @@ -5352,6 +5352,74 @@ path from <math>s</math> to <math>w</math>.</p>
</li>
</list>

### 17.3 Dijkstra's Algorithm

Java

```Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.PriorityQueue;

public class Dijkstra {

private final double[] distTo;
private final DirectedEdge[] edgeTo;
private final boolean[] marked;
private final int source;

public Dijkstra(EdgeWeightedDigraph G, int s) {
source = s;
distTo = new double[G.V()];
edgeTo = new DirectedEdge[G.V()];
marked = new boolean[G.V()];
Arrays.fill(distTo, Double.POSITIVE_INFINITY);
distTo[s] = 0.0;

PriorityQueue<Integer> pq = new PriorityQueue<>((v1, v2) -> Double.compare(distTo[v1], distTo[v2]));
pq.offer(s);
while (!pq.isEmpty()) {
int v = pq.poll();
marked[v] = true;
for (DirectedEdge e : G.adj(v)) {
relax(e, pq);
}
}
}

private void relax(DirectedEdge e, PriorityQueue<Integer> pq) {
int v = e.from(), w = e.to();
if (distTo[w] > distTo[v] + e.weight()) {
distTo[w] = distTo[v] + e.weight();
edgeTo[w] = e;
if (marked[w]) {
pq.offer(w);
} else {
pq.offer(w);
}
}
}

public double distTo(int v) {
return distTo[v];
}

public Iterable<DirectedEdge> pathTo(int v) {
if (!hasPathTo(v)) return null;
List<DirectedEdge> path = new ArrayList<>();
for (DirectedEdge e = edgeTo[v]; e != null; e = edgeTo[e.from()]) {
path.add(e);
}
return path;
}

public boolean hasPathTo(int v) {
return distTo[v] < Double.POSITIVE_INFINITY;
}
}
```

## 18 Substring Search

### 18.1 Introduction
Expand Down

0 comments on commit 00665e6

Please sign in to comment.