-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph.java
80 lines (64 loc) · 1.77 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
class Graph {
public int numVertex;
public String[] vertices;
public Edge edges[];
public HashTable hashtable;
public Graph(int numVerteces, String[] vertices) {
this.numVertex = numVerteces;
this.vertices = vertices;
this.edges = new Edge[numVerteces];
int i;
for (i = 0; i < numVertex; i++) {
edges[i] = null;
}
hashtable = new HashTable(vertices);
}
public void insert(String origin, String destination, int cost) {
// System.out.println("Call to insert with " + origin + " " +
// destination + " " + cost);
Edge start = edges[lookUp(origin)];
Edge newEdge = new Edge(lookUp(destination), null, cost);
newEdge.next = start;
edges[lookUp(origin)] = newEdge;
}
public void print() {
int i;
for (i = 0; i < numVertex; i++) {
System.out.print("Vertex " + i + " Adj. List:");
if (edges[i] == null) {
System.out.println("<empty>");
} else {
for (Edge tmp = edges[i]; tmp != null; tmp = tmp.next) {
System.out.print(" " + tmp.neighbor);
}
System.out.println();
}
}
}
public int lookUp(String orig) {
return hashtable.find(orig);
}
public void printLookupList() {
for (int i = 0; i < vertices.length; i++) {
System.out.println((i < 10 ? " " : "") + i + ": " + vertices[i]);
}
}
public void printAdjacencyList() {
for (int i = 0; i < edges.length; i++) {
Edge tmp = edges[i];
String concat = i + ": ";
if (i < 10) {
concat = " " + concat;
}
Edge tmp2 = tmp;
while (tmp2 != null) {
concat += "[" + tmp2.neighbor + "|" + tmp2.cost + "] ->";
tmp2 = tmp2.next;
}
System.out.println(concat + (tmp == null ? "" : " ") + "[null]");
}
}
public Edge get(int vertice) {
return edges[vertice];
}
}