-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.java
More file actions
103 lines (76 loc) · 2.19 KB
/
Copy pathgraph.java
File metadata and controls
103 lines (76 loc) · 2.19 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import java.util.*;
public class graph {
int V = 0;
LinkedList<Integer> adj[];
Stack<Integer> stack = new Stack<>();
public graph(int ver) {
V = ver;
adj = new LinkedList[ver];
for (int i = 0; i < V; i++) {
adj[i] = new LinkedList<>();
}
}
public void addEdge(int v, int n) {
adj[v].add(n);
}
public void DFS(boolean[] vis, int vertex) {no
vis[vertex] = true;
System.out.println(vertex);
// loop through all Vertex connections
for (Integer newV : adj[vertex]) {
if (vis[newV] == false) {
DFS(vis, newV);
}
}
stack.add(vertex);
}
public graph transpose() {
graph newGraph = new graph(V);
for (int i = 0; i < V; i++) {
Iterator<Integer> t = adj[i].listIterator();
while (t.hasNext()) {
newGraph.adj[t.next()].add(i);
}
}
return newGraph;
}
public void altDFS(boolean[] vis, int vertex) {
vis[vertex] = true;
System.out.println(vertex);
// loop through all Vertex connections
for (Integer newV : adj[vertex]) {
if (vis[newV] == false) {
DFS(vis, newV);
}
}
}
public void fullConstrained() {
//call dsf
//empty list
boolean[] vis = new boolean[V];
for (int i = 0; i < vis.length; i++) {
vis[i] = false;
}
System.out.println("------DSF first time--------");
//start dsf on 0
for (int i = 0; i < V ; i++) {
if(vis[i] == false){
DFS(vis, i);
}
}
System.out.println("-----DSF seconds time---------");
//transpose graphs
graph newGraph = transpose();
//run dsf on stack on transposed graphs
for (int i = 0; i < vis.length; i++) {
vis[i] = false;
}
while (!stack.isEmpty()) {
int next = stack.pop();
if (vis[next] == false) {
newGraph.altDFS(vis, next); //need non general form, pop off when using
System.out.println("--");
}
}
}
}