Repository navigation
Expand file tree
/
Copy pathGraph.java
More file actions
68 lines (55 loc) · 1.53 KB
/
Copy pathGraph.java
File metadata and controls
68 lines (55 loc) · 1.53 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
import java.util.LinkedList;
public class Graph {
class Edge {
int destination;
int distance;
public Edge(int des, int dis){
this.destination = des;
this.distance = dis;
}
public int getDes(){
return destination;
}
public int getDis(){
return distance;
}
}
private int nVertices;
private LinkedList<Edge>[] adjacencyList;
private String[] vertices;
public Graph(int nVertices, String[] vertices){
this.nVertices = nVertices;
this.vertices = vertices;
this.adjacencyList = new LinkedList[nVertices];
for(int i = 0; i < nVertices; i++){
adjacencyList[i] = new LinkedList<>();
}
}
public int getNumVertices(){
return nVertices;
}
public LinkedList<Edge> getAdjacentEdges(int vertex) {
return adjacencyList[vertex];
}
public String getVertexName(int index) {
if (index >= 0 && index < vertices.length) {
return vertices[index];
}
return null;
}
public String[] getVertices(){
return vertices;
}
public void addEdge(int a, int b, int distance){
adjacencyList[a].add(new Edge(b, distance));
adjacencyList[b].add(new Edge(a, distance));
}
public int getIndex(String name){
for (int i = 0; i < vertices.length; i++) {
if (vertices[i].equals(name)) {
return i;
}
}
return -1;
}
}