Skip to content

Commit c703771

Browse files
committed
Completed Dijkstra
1 parent 72ce3d4 commit c703771

File tree

3 files changed

+78
-60
lines changed

3 files changed

+78
-60
lines changed

Algorithms/Dijkstra.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66

77
import java.util.HashMap;
88
import java.util.Map;
9+
import java.util.Set;
910

1011
/**
1112
* Created by Uzumaki Naruto on 2/17/2017.
13+
*
1214
*/
1315
public class Dijkstra<T> {
1416
private Graph<T> graph;
@@ -17,15 +19,50 @@ public class Dijkstra<T> {
1719
this.graph = graph;
1820
}
1921

22+
public static void main(String[] args) {
23+
Graph<Integer> graph = new Graph<>(4, false);
24+
graph.addEdge(0, 1, 101);
25+
graph.addEdge(0, 3, 1);
26+
graph.addEdge(0, 2, 101);
27+
graph.addEdge(1, 2, 1);
28+
graph.addEdge(1, 3, 10);
29+
graph.addEdge(2, 3, 10);
30+
System.out.println(new Dijkstra<Integer>(graph).shortestPaths(0));
31+
}
32+
2033
public Map<T, Integer> shortestPaths(T from) {
2134
Heap<T> heap = new Heap<T>();
2235
Map<T, Integer> shortestPath = new HashMap<T, Integer>();
36+
Set<T> verticesSet = graph.verticesSet();
37+
38+
// remove the initial from node
39+
verticesSet.remove(from);
40+
41+
// Initially all are Infinity
2342
for (T node : graph.verticesSet()) {
2443
heap.add(new GraphNode<T>(node, Integer.MAX_VALUE));
2544
}
45+
46+
// update for initial node
2647
for (T adjNode : graph.adjacencyList(from)) {
2748
heap.alterPriority(adjNode, graph.edgeWeight(from, adjNode));
2849
}
29-
return null;
50+
51+
while (!verticesSet.isEmpty()) {
52+
GraphNode<T> curr = heap.poll();
53+
shortestPath.put(curr.getNodeName(), curr.getWeight());
54+
int minDistanceToCurrNode = curr.getWeight();
55+
verticesSet.remove(curr.getNodeName());
56+
for (T node : graph.adjacencyList(curr.getNodeName()))
57+
if (verticesSet.contains(node)) {
58+
int currWeight = heap.getPriority(node);
59+
int candidateVal = minDistanceToCurrNode + graph.edgeWeight(curr.getNodeName(), node);
60+
if (currWeight > candidateVal) {
61+
heap.alterPriority(node, candidateVal);
62+
}
63+
}
64+
}
65+
66+
return shortestPath;
3067
}
3168
}

DataStructures/Graph.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
/**
66
* Created by Mayur Kulkarni on 2/5/2017.
7+
*
78
*/
89
public class Graph<T> implements Iterable<T> {
910
/**
@@ -79,8 +80,8 @@ public int edgeWeight(T fromNode, T toNode) {
7980
/**
8081
* Inserts toNode to the adjacency list of fromNode
8182
*
82-
* @param fromNode from node
83-
* @param toNode to node
83+
* @param fromNode from node
84+
* @param toNode to node
8485
*/
8586
private void insertIntoGraphMap(T fromNode, T toNode) {
8687
graph.compute(fromNode, (key, value) -> {
@@ -137,7 +138,7 @@ public void addEdge(T fromNode, T toNode, int weight) {
137138
/**
138139
* Returns the adjacency list of the graph
139140
* @param key the index of node you're interested in
140-
* @return adjacency list of node specifief in key
141+
* @return adjacency list of node specific in key
141142
*/
142143
public List<T> adjacencyList(T key) {
143144

@@ -170,11 +171,11 @@ public Set<T> articulationPoints() {
170171
* 2. if current node is root node of DFS, and has more than 2 independent children
171172
* for more: https://adventinprogramming.wordpress.com/2017/02/05/articulation-points-in-graph/
172173
*
173-
* @param visited denotes the nodes which are already visited
174-
* @param visitedTime denotes the order in which nodes are visited
175-
* @param currNode the current node of this traversa;
176-
* @param lowTime denotes the minimum lowTime of adjacent vertices
177-
* @param parent parent of the current node
174+
* @param visited denotes the nodes which are already visited
175+
* @param visitedTime denotes the order in which nodes are visited
176+
* @param currNode the current node of this traversal;
177+
* @param lowTime denotes the minimum lowTime of adjacent vertices
178+
* @param parent parent of the current node
178179
* @param articulationPoints articulation points of the current graph
179180
*/
180181
public void DFSArticulationPoint(Set<T> visited,
@@ -198,10 +199,12 @@ public void DFSArticulationPoint(Set<T> visited,
198199
if (visitedTime.get(currNode) <= lowTime.get(adjNode)) {
199200
isArticulationPoint = true;
200201
} else {
201-
lowTime.compute(currNode, (node, nodeLowTime) -> Math.min(lowTime.get(currNode), lowTime.get(adjNode)));
202+
lowTime.compute(currNode, (node, nodeLowTime) ->
203+
Math.min(lowTime.get(currNode), lowTime.get(adjNode)));
202204
}
203205
} else {
204-
lowTime.compute(currNode, (node, lowtime) -> Math.min(lowTime.get(currNode), lowTime.get(adjNode)));
206+
lowTime.compute(currNode, (node, lowtime) ->
207+
Math.min(lowTime.get(currNode), lowTime.get(adjNode)));
205208
}
206209
}
207210
// first condition is satisfied only by root nodes of tarjanSCC, for rest of them there's
@@ -216,9 +219,9 @@ public void DFSArticulationPoint(Set<T> visited,
216219
* Tell whether the currNode is the root of the tarjanSCC or not.
217220
* The root of the tarjanSCC will have null as it's parent
218221
*
219-
* @param currNode The node you're interested in
220-
* @param parent parent HashMap who's key is node and value is it's parent
221-
* @return
222+
* @param currNode The node you're interested in
223+
* @param parent parent HashMap who's key is node and value is it's parent
224+
* @return or not it is root
222225
*/
223226
private boolean isRoot(T currNode, Map<T, T> parent) {
224227
return parent.get(currNode) == null;

Random/Demo.java

Lines changed: 24 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,33 @@
11
package Random;
22

3-
4-
import Utils.SearchUtils;
5-
6-
import java.util.ArrayList;
7-
import java.util.List;
8-
import java.util.Random;
9-
10-
/**
11-
* Created by mayur on 4/9/16.
12-
*/
133
public class Demo {
14-
public static void main(String args[]) {
15-
Random random = new Random();
16-
List<Double> normal = new ArrayList<>();
17-
List<Double> stream = new ArrayList<>();
18-
for (int i = 0; i < 10000; i++) {
19-
int size = random.nextInt(100000) + 10;
20-
int[] arr = new int[size];
21-
for (int j = 0; j < size; j++) {
22-
arr[j] = random.nextInt(Integer.MAX_VALUE);
23-
}
24-
double startTime = System.currentTimeMillis();
25-
int max = SearchUtils.maxInArray(arr);
26-
double normalTime = System.currentTimeMillis() - startTime;
27-
startTime = System.currentTimeMillis();
28-
int maxStream = SearchUtils.maxInArrayStream(arr);
29-
double streamTime = System.currentTimeMillis() - startTime;
30-
assert max == maxStream;
31-
normal.add(normalTime);
32-
stream.add(streamTime);
33-
}
34-
printStatistics(normal, stream);
4+
public static void main(String[] args) {
5+
ListNode myList = new Demo().new ListNode();
6+
myList.node = 1;
7+
myList.next = new Demo().new ListNode();
8+
myList.next.node = 2;
9+
myList.next.next = new Demo().new ListNode();
10+
myList.next.next.node = 3;
11+
new Demo().reverse(null, myList);
12+
System.out.println(myList.node + " " + myList.next.node + " " + myList.next.next.node);
3513
}
3614

37-
public static double average(List<Double> list) {
38-
double sum = 0;
39-
for (double a : list) {
40-
sum += a;
15+
public void reverse(ListNode prev, ListNode currNode) {
16+
if (currNode == null) {
17+
return;
18+
} else {
19+
reverse(currNode, currNode.next);
20+
currNode.next = prev;
4121
}
42-
return sum / list.size();
4322
}
4423

45-
private static void printStatistics(List<Double> normal, List<Double> stream) {
46-
System.out.println("Normal: ");
47-
System.out.println("Average: " + average(normal) + " ms");
48-
System.out.println("Worst: " + normal.stream().mapToDouble((x) -> x).max().getAsDouble());
49-
System.out.println("Best: " + normal.stream().mapToDouble((x) -> x).min().getAsDouble());
50-
System.out.println("Stream: ");
51-
System.out.println("Average: " + average(stream) + " ms");
52-
System.out.println("Worst: " + stream.stream().mapToDouble((x) -> x).max().getAsDouble());
53-
System.out.println("Best: " + stream.stream().mapToDouble((x) -> x).min().getAsDouble());
24+
class ListNode {
25+
int node;
26+
ListNode next;
27+
28+
@Override
29+
public String toString() {
30+
return String.valueOf(this.node);
31+
}
5432
}
55-
}
33+
}

0 commit comments

Comments
 (0)