-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdjacencyListWeightedGraph.java
More file actions
42 lines (42 loc) · 1.35 KB
/
AdjacencyListWeightedGraph.java
File metadata and controls
42 lines (42 loc) · 1.35 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
import java.util.*;
public class AdjacencyListWeightedGraph {
int vertices;
int edges;
public static class Edge{
int to;
int weight;
public Edge(int to, int weight){
this.to = to;
this.weight = weight;
}
}
@SuppressWarnings("unchecked")
List<Edge>[] adjacencyList = new ArrayList[vertices];
public void addEdge(int u, int v, int weight){
adjacencyList[u].add(new Edge(v, weight));
adjacencyList[v].add(new Edge(u, weight));
edges++;
}
public static void main(String[] args){
AdjacencyListWeightedGraph graph = new AdjacencyListWeightedGraph();
graph.vertices = 5;
graph.edges = 0;
graph.adjacencyList = new ArrayList[graph.vertices];
for(int i=0; i<graph.vertices; i++){
graph.adjacencyList[i] = new ArrayList<>();
}
graph.addEdge(0, 1, 10);
graph.addEdge(0, 4, 20);
graph.addEdge(1, 4, 30);
graph.addEdge(1, 3, 40);
graph.addEdge(1, 2, 50);
graph.addEdge(2, 3, 60);
for(int i=0; i<graph.vertices; i++){
System.out.print(i + ": ");
for(Edge edge : graph.adjacencyList[i]){
System.out.print("-> (to: " + edge.to + ", weight: " + edge.weight + ") ");
}
System.out.println();
}
}
}