forked from TECHOUS/DSKaKhel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph-Representation.java
More file actions
64 lines (44 loc) · 1.72 KB
/
Graph-Representation.java
File metadata and controls
64 lines (44 loc) · 1.72 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
package GRAPH.Java;
import java.util.*;
import jdk.internal.org.jline.utils.InputStreamReader;
import java.io.*;
// Adjancy List Representation of Graph
class Graph-Representation{
static class Edge{
int src; // source
int nbr; // neighbour
int w; // weight
Edge(int src,int nbr, int w){
this.src = src;
this.nbr = nbr;
this.w = w;
}
public static void main(String[] args){
BufferedOutputStream br = new BufferedReader(new InputStreamReader(System.in));
// number of vertex
int size = 6;
// graph represent as array of arraylist of edges
ArrayList<Edge>[] graph = new ArrayList<Edge>[size];
// create list for each vertex
for(int i=0;i<size;i++)
{
graph[i] = new ArrayList<Edge>();
}
// here in first array 0 is connected with 3 with weight 10
// likewise 0 connected with 1 with weight 50......
graph[0].add(new Edge(0, 3, 10));
graph[0].add(new Edge(0, 1, 50));
graph[1].add(new Edge(1, 0, 50));
graph[1].add(new Edge(1, 2, 54));
graph[2].add(new Edge(2, 0, 23));
graph[2].add(new Edge(2, 2, 34));
graph[2].add(new Edge(2, 4, 52));
graph[3].add(new Edge(3, 5, 10));
graph[3].add(new Edge(3, 4, 40));
graph[4].add(new Edge(4, 3, 67));
graph[4].add(new Edge(4, 1, 89));
graph[5].add(new Edge(5, 2, 30));
graph[5].add(new Edge(5, 1, 20));
}
}
}