-
-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathWeightedGraph.java
More file actions
79 lines (64 loc) · 2.26 KB
/
Copy pathWeightedGraph.java
File metadata and controls
79 lines (64 loc) · 2.26 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
71
72
73
74
75
76
77
78
79
package util;
import java.util.*;
public class WeightedGraph {
private int vertices;
private List<GraphEdge>[] adjacencyList;
public WeightedGraph(int vertices) {
this.vertices = vertices;
adjacencyList = new ArrayList[vertices];
for (int i = 0; i < vertices; i++)
adjacencyList[i] = new ArrayList<>();
}
public void addDirectedEdge(int source, int destination, int weight) {
GraphEdge edge = new GraphEdge(source, destination, weight);
adjacencyList[source].add(edge);
}
public void addUndirectedEdge(int source, int destination, int weight) {
GraphEdge sourceToDestinationEdge = new GraphEdge(source, destination, weight);
GraphEdge destinationToSourceEdge = new GraphEdge(destination, source, weight);
adjacencyList[source].add(sourceToDestinationEdge);
adjacencyList[destination].add(destinationToSourceEdge);
}
// Code to remove a directed edge
public void removeDirectedEdge(int source, int destination) {
for (GraphEdge edge : adjacencyList[source])
if (edge.getDestination() == destination) {
adjacencyList[source].remove(edge);
break;
}
}
// Code to remove an undirected edge
public void removeUndirectedEdge(int source, int destination) {
removeDirectedEdge(source, destination);
removeDirectedEdge(destination, source);
}
public List<GraphEdge>[] getVertices() {
return adjacencyList;
}
public static void main(String[] args) {
// Creating a graph that has 5 vertices
// 0 2
// | \ /
// | \ /
// 4| \ 6 / 8
// | \ /
// | 1 \ /
// 1------4 3
// \ 7 /
// ----------------------
WeightedGraph graph = new WeightedGraph(5);
graph.addUndirectedEdge(0,1,4);
graph.addUndirectedEdge(0,4,6);
graph.addUndirectedEdge(1,4,1);
graph.addUndirectedEdge(1,3,7);
graph.addUndirectedEdge(4,2,8);
// Code to print the graph
List<GraphEdge>[] vertices = graph.getVertices();
for (int i = 0; i < vertices.length; i++) {
System.out.print("Vertex " + i + " is connected to: ");
for (GraphEdge edge : vertices[i])
System.out.print(edge.getDestination() + " ");
System.out.println();
}
}
}