- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.java
More file actions
Latest commit
69 lines (57 loc) · 1.87 KB
/
Copy pathGraph.java
File metadata and controls
69 lines (57 loc) · 1.87 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
importjava.util.HashMap;
importjava.util.Iterator;
importjava.util.NoSuchElementException;
/**
* This class represent the Graph on which the Dijkstra's SSSP algorithm is run
*/
/**
* @author PRITI
*
*/
publicclassGraph {
//storing the adjacency list of each individual nodes
publicfinalHashMap<Integer, HashMap<Integer, Double>> fullGraphList = newHashMap<Integer, HashMap<Integer, Double>>();
//no. of nodes in the graph
intnodeNum;
//parameterized constructor for creating HashMaps for adjacency list of each individual graph node
Graph(intnum)
{
this.nodeNum = num;
for(inti=0;i<nodeNum;i++)
fullGraphList.put(newInteger(i), newHashMap<Integer, Double>());
return;
}
//this method is called after each line is read from the input file and creates edges between the specified nodes
//for both the nodes to achieve an undirected graph
publicvoidaddCost(IntegernodeFrom, IntegernodeTo, Doublecost)
{
if (!fullGraphList.containsKey(nodeFrom) || !fullGraphList.containsKey(nodeTo))
thrownewNoSuchElementException("No such node in the graph");
fullGraphList.get(nodeFrom).put(nodeTo, cost);
fullGraphList.get(nodeTo).put(nodeFrom, cost);
}
//this method return the complete adjacency list representation of the graph for each individual nodes
HashMap<Integer, HashMap<Integer, Double>> getAdjList()
{
returnfullGraphList;
}
//this method prints the calling graph instance
voidprint_Graph()
{
for(Integern : fullGraphList.keySet())
{
HashMap<Integer, Double> h = fullGraphList.get(n);
for(Integern1: h.keySet())
{
System.out.println(n.intValue()+" -> "+n1.intValue() + " " + h.get(n1));
}
}
}
/**
* @return iterator for the adjacency list of the graph
*/
publicIterator<Integer> iterator() {
// TODO Auto-generated method stub
returnfullGraphList.keySet().iterator();
}
}