-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseparateComponent.java
More file actions
60 lines (53 loc) · 1.54 KB
/
separateComponent.java
File metadata and controls
60 lines (53 loc) · 1.54 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
package algorithm.graph;
import java.util.LinkedList;
/**
* The type Separate component.
*/
public class separateComponent {
/**
* Separate components boolean.
*
* @param graph the graph
* @param source the source
* @param destination the destination
* @return the boolean
*/
public static boolean separateComponents(Graph graph,int source,int destination){
graph.getAdj()[source].remove(graph.getAdj()[source].indexOf(destination));
return !isConnected( graph);
}
/**
* Is connected boolean.
*
* @param graph the graph
* @return the boolean
*/
private static boolean isConnected(Graph graph) {
int vertices = graph.getVertices();
boolean[] visited = new boolean[vertices];
graph.dfsTraversal(0,visited);
for(int i=0;i<vertices;i++)
if(!visited[i])
return false;
return true;
}
/**
* Main.
*
* @param args the args
*/
public static void main(String args[]) {
Graph g = new Graph(5);
g.addEdge(0, 1);
g.addEdge(1, 2);
g.addEdge(2, 3);
g.addEdge(3, 4);
g.addEdge(0, 4);
// remove edge 3 -> 4
Object x = separateComponents(g, 3, 4);
System.out.println("Separate components created due to deletion of edge 3 -> 4? " + x);
// remove edge 1 -> 2
x = separateComponents(g, 1, 2);
System.out.println("Separate components created due to deletion of edge 1 -> 2? " + x);
}
}