-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountEdge.java
More file actions
63 lines (55 loc) · 1.37 KB
/
CountEdge.java
File metadata and controls
63 lines (55 loc) · 1.37 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
package datastructure.graph;
/**
* The type Count edge.
*/
public class CountEdge {
/**
* Count int.
*
* @param graph the graph
* @return the int
*/
public static int count(AdjacencyListGraph graph){
int count =0;
int vertices = graph.vertices;
for(int i=0;i<vertices;i++){
count+=graph.adjacencyList[i].size();
}
//return count as this is a directed graph.
return count;
}
/**
* Main.
*
* @param args the args
*/
public static void main(String args[]) {
AdjacencyListGraph g = new AdjacencyListGraph(9);
g.addEdge(0,2);
g.addEdge(0,5);
g.addEdge(2,3);
g.addEdge(2,4);
g.addEdge(5,3);
g.addEdge(5,6);
g.addEdge(3,6);
g.addEdge(6,7);
g.addEdge(6,8);
g.addEdge(6,4);
g.addEdge(7,8);
g.printGraph();
System.out.println("Number of edges: " + count(g));
System.out.println();
AdjacencyListGraph g2 = new AdjacencyListGraph(7);
g2.addEdge(1,2);
g2.addEdge(1,3);
g2.addEdge(3,4);
g2.addEdge(3,5);
g2.addEdge(2,5);
g2.addEdge(2,4);
g2.addEdge(4,6);
g2.addEdge(4,5);
g2.addEdge(6,5);
g2.printGraph();
System.out.println("Number of edges: " + count(g2));
}
}