-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.java
More file actions
70 lines (59 loc) · 1.81 KB
/
Copy pathGraph.java
File metadata and controls
70 lines (59 loc) · 1.81 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
70
import java.util.LinkedList;
public class Graph {
// Keep track of the graph size
private int numVertices;
private int numEdges;
private LinkedList<node>[] adjacencylist;
public Graph(int numVertices, int numEdges) {
this.numVertices = numVertices;
this.numEdges = numEdges;
//initialize adjacency lists for all the vertices
this.adjacencylist = new LinkedList[numVertices];
for (int i = 0; i < numVertices; i++) {
adjacencylist[i] = new LinkedList<>();
}
}
public void printGraph() {
for (int i = 0; i < this.numVertices; i++) {
LinkedList<node> list = this.adjacencylist[i];
for (int j = 0; j < list.size(); j++) {
System.out.println("vertex-" + i + " is connected to " +
list.get(j).destination + " with weight " + list.get(j).weight);
}
}
}
public int getNumVertices(){
return this.numVertices;
}
public int numEdges(){
return this.numEdges;
}
public int getWeight(int source,int destination){
if (source==destination)
return 0;
LinkedList<node> list = this.adjacencylist[source];
for (int j = 0; j < list.size(); j++) {
if(list.get(j).destination == destination)
return list.get(j).weight;
}
return Integer.MAX_VALUE;
}
public void setEgde(int source, int destination, int weight) {
node edge = new node(source, destination, weight);
this.adjacencylist[source].addLast(edge);
}
public void deleteEgde(int source, int destination) {
for (int i = 0; i < this.adjacencylist[source].size(); i++) {
if (this.adjacencylist[source].get(i).destination == destination){
this.adjacencylist[source].remove(i);
break;
}
}
}
public LinkedList<node> getList(int index){
return this.adjacencylist[index];
}
public int getAdjList(){
return this.adjacencylist.length;
}
}