-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.java
More file actions
111 lines (95 loc) · 3.31 KB
/
Graph.java
File metadata and controls
111 lines (95 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package axcruzlopez;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import axcruzlopez.Edge;
import axcruzlopez.Node;
import java.util.PriorityQueue;
public class Graph {
// Key is the vertices structured as a NODE.
// Value is an arraylist of it's edges that contains (source, destination, weight)
// HashMap<Node, ArrayList<Edge>> adjacencyList;
HashMap<Integer, Node> nodes;
HashMap<Node, ArrayList<Edge>> adjacencyList;
/*
key = 0 value = {(0, 1, 8), (0, 2 ,8), (0, 7, 4)}
key = 1 value = {(0, 1, 8), (
*/
public Graph() {
this.nodes = new HashMap<>();
this.adjacencyList = new HashMap<>();
}
public Boolean addVertex(int vertex) {
if(!nodes.containsKey(vertex)) {
Node vertexNode = new Node(vertex);
nodes.put(vertex, vertexNode);
adjacencyList.put(vertexNode, new ArrayList<Edge>());
return true;
}
return false;
}
public Boolean addEdge(int source, int destination, int weight ) {
Node vertexNode = nodes.get(source);
Node destinationNode = nodes.get(destination);
if(vertexNode != null && destinationNode != null) {
adjacencyList.get(vertexNode).add(new Edge(source, destination, weight));
adjacencyList.get(destinationNode).add(new Edge(destination, source, weight));
return true;
}
return false;
}
public void calculatePaths(int source) {
PriorityQueue<Node> unVisited = new PriorityQueue<>(); /// destinations unexplored
ArrayList<Node> visited = new ArrayList<>();
for(int key : this.nodes.keySet()) {
Node currentV = this.nodes.get(key);
if(currentV.getValue() == source) {
currentV.setDistance(0);
} else {
currentV.setDistance(Integer.MAX_VALUE);
}
currentV.setPrev(null);
unVisited.add(currentV);
}
Node currentV;
while(!unVisited.isEmpty()) {
currentV = unVisited.poll();
if(visited.contains(currentV) && !unVisited.contains(currentV)) {
return;
}
System.out.println("vertex: " + currentV.getValue());
for(Edge edge : adjacencyList.get(currentV)) {
// System.out.println("Edge: " + edge.destination);
Node adjV = this.nodes.get(edge.getDestination());
// System.out.println(adjV.getValue());
// System.out.println(edgeWeight);
int edgeWeight = edge.getWeight();
int alternativePath = currentV.getDistance() + edgeWeight;
if (alternativePath < adjV.getDistance()) {
adjV.setDistance(alternativePath);
adjV.setPrev(currentV);
unVisited.add(adjV);
}
}
visited.add(currentV);
}
}
public void shortestPath(int source) {
Node findMe = this.nodes.get(source);
List<Node> path = new ArrayList<>();
for(Node node = findMe; node != null; node = node.getPrev()) {
path.add(node);
}
Collections.reverse(path);
for(Node node : path) {
System.out.print(node.getValue() + " -> ");
}
System.out.println(" Total Distance: " + findMe.getDistance());
}
public void printGraph() {
for(int key: this.nodes.keySet()) {
System.out.println("Vertex: " + key + " : Edges: " + this.adjacencyList.get(this.nodes.get(key)).toString());
}
}
}