-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.java
More file actions
69 lines (57 loc) · 1.87 KB
/
Graph.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
import java.util.HashMap;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* This class represent the Graph on which the Dijkstra's SSSP algorithm is run
*/
/**
* @author PRITI
*
*/
public class Graph {
//storing the adjacency list of each individual nodes
public final HashMap<Integer, HashMap<Integer, Double>> fullGraphList = new HashMap<Integer, HashMap<Integer, Double>>();
//no. of nodes in the graph
int nodeNum;
//parameterized constructor for creating HashMaps for adjacency list of each individual graph node
Graph(int num)
{
this.nodeNum = num;
for(int i=0;i<nodeNum;i++)
fullGraphList.put(new Integer(i), new HashMap<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
public void addCost(Integer nodeFrom, Integer nodeTo, Double cost)
{
if (!fullGraphList.containsKey(nodeFrom) || !fullGraphList.containsKey(nodeTo))
throw new NoSuchElementException("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()
{
return fullGraphList;
}
//this method prints the calling graph instance
void print_Graph()
{
for(Integer n : fullGraphList.keySet())
{
HashMap<Integer, Double> h = fullGraphList.get(n);
for(Integer n1: h.keySet())
{
System.out.println(n.intValue()+" -> "+n1.intValue() + " " + h.get(n1));
}
}
}
/**
* @return iterator for the adjacency list of the graph
*/
public Iterator<Integer> iterator() {
// TODO Auto-generated method stub
return fullGraphList.keySet().iterator();
}
}