-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph.java
157 lines (145 loc) · 4.67 KB
/
Graph.java
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import java.io.*;
import java.lang.*;
import java.util.*;
import java.math.*;
public class Graph{
int numOfVertices;
int total_vertices;
HashMap<Integer,GraphNode> vertexSet;
public Graph(){
numOfVertices = 0;
vertexSet = new HashMap<Integer,GraphNode>();
total_vertices = 0;
//System.out.println("New Graph Created");
}
public void addVertex(int id){
if(!vertexSet.containsKey(id)){
GraphNode newVertex = new GraphNode(id);
vertexSet.put(id,newVertex);
numOfVertices++;
//System.out.println("New vertex added with id: "+id+" total vetices: "+numOfVertices);
}
}
public void resetAllDistances(){
// Sets all the distances in the graph to infinty
for(int nodeId: vertexSet.keySet()){
vertexSet.get(nodeId).resetDistance();
}
//System.out.println("All vertex distances set to infinty");
}
public void populate(String input){
// reads stuff from input file and populates the graph
try{
Scanner in = new Scanner(new File(input));
int inputNumOfVertices = in.nextInt();
int inputNumOfEdges = in.nextInt();
total_vertices = inputNumOfVertices;
for(int i=0;i<inputNumOfEdges;i++){
int vertex1 = in.nextInt();
int vertex2 = in.nextInt();
long weight = in.nextLong();
addVertex(vertex1);
addVertex(vertex2);
vertexSet.get(vertex1).addNeighbor(vertex2,weight);
vertexSet.get(vertex2).addNeighbor(vertex1,weight);
}
}
catch(Exception e){
System.out.println("File not found: "+input);
}
//System.out.println("Vertices in graph: " + numOfVertices + " = "+total_vertices);
}
public void populate_ip(String input){
// reads stuff from input file and populates the graph
try{
Scanner in = new Scanner(new File(input));
for(int i=0;i<total_vertices;i++){
String ip;
while(true){
// read next ip from file
ip = in.nextLine();
if(ip.contains("."))
break;
}
vertexSet.get(i).setIp(ip);
}
}
catch(Exception e){
System.out.println("File not found: "+input);
}
}
public long dijkstraSSP(int source,int destination){
// Implements dijkstra ssp using Fibonacci;
resetAllDistances();
vertexSet.get(source).setDistance(0);
HashSet<Integer> visited = new HashSet<Integer>();
//PriorityQueue<GraphNode> heap = new PriorityQueue<GraphNode>();
FibonacciHeap heap = new FibonacciHeap();
GraphNode start_node = vertexSet.get(source);
start_node.path = Integer.toString(start_node.id);
heap.insert(start_node);
visited.add(start_node.id);
long count = 0;
while(heap.head != null){
//GraphNode min = heap.poll();
GraphNode min = heap.removeMin();
//count++;
//System.out.println(count);
if(min.id == destination){
//System.out.println("Shortest Distance between node: "+source+" and node: "+destination+" is: "+min.distance+"\n with path: "+min.path);
return min.distance;
//break;
}
//visited.add(min.id);
for(AdjacencyNode neighbor: min.adjacencyList){
GraphNode neighborNode = vertexSet.get(neighbor.vertexId);
if(!visited.contains(neighborNode.id)){
heap.insert(neighborNode);
visited.add(neighborNode.id);
}
long edge_weight = neighbor.weight;
long new_distance = min.distance + edge_weight;
long old_distance = neighborNode.distance;
if(new_distance < old_distance){
/*if(old_distance != Long.MAX_VALUE){
heap.remove(neighborNode);
}*/
//neighborNode.distance = new_distance;
neighborNode.path = min.path + " " + Integer.toString(neighborNode.id);
//heap.add(neighborNode);
heap.decreaseKey(neighborNode.id, new_distance);
}
}
}
return 0;
}
public void dijkstraSSP(int source){
// This calculates all the shortest paths to all other nodes
dijkstraSSP(source, -1);
//System.out.println("All shortest paths from source: "+source+" calulated");
}
public void createRoutingTable(int id){
GraphNode vertex = vertexSet.get(id);
vertex.routing_table = new Trie();
for(int i=0;i < total_vertices; i++){
if(i == id)
continue;
GraphNode destination = vertexSet.get(i);
Scanner in = new Scanner(destination.path);
in.nextInt();
int next_hop = in.nextInt();
//System.out.println("Inserting for source: "+id+" destination: "+destination.id+" next_hop: "+next_hop+" path: "+destination.path);
vertex.routing_table.insert(destination.ip_binary, next_hop);
}
vertex.routing_table.prune();
//System.out.println(id+"'s routing table is initialized");
}
public void route(int source, int destination){
int node = source;
GraphNode destination_node = vertexSet.get(destination);
while(node != destination){
GraphNode curren_node = vertexSet.get(node);
node = curren_node.routing_table.find(destination_node.ip_binary);
}
}
}