-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTopologicalSort.java
More file actions
158 lines (138 loc) · 5.11 KB
/
Copy pathTopologicalSort.java
File metadata and controls
158 lines (138 loc) · 5.11 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package com.shivapreals;
import com.sun.corba.se.impl.ior.NewObjectKeyTemplateBase;
import java.util.*;
class Graph {
Stack<Integer> toppologicalSort=new Stack<>();
Map<Integer, List<Integer>> edges = new HashMap<Integer, List<Integer>>();
int numOfVertices = 6;
int[] indegree = new int[numOfVertices];
int[] visitedNodes = new int[numOfVertices];
public void addEdge(int source, int destination) {
List<Integer> destinations = edges.get(source);
if (destinations == null) {
destinations = new ArrayList<Integer>();
}
destinations.add(destination);
edges.put(source, destinations);
}
public void calculateMyDegree() {
for (int i = 0; i < 5; i++) {
indegree[i] = 0;
}
Iterator<Integer> edgeIt = edges.keySet().iterator();
while (edgeIt.hasNext()) {
int currentSource = edgeIt.next();
List<Integer> destinationList = edges.get(currentSource);
traverseNeighbours(currentSource, destinationList);
}
}
public void traverseNeighbours(int currentVertex, List<Integer> destinationList) {
for (int i = 0; i < destinationList.size(); i++) {
int currentDestination = 0;
try {
currentDestination = destinationList.get(i);
System.out.println(currentDestination);
} catch (Exception e) {
System.out.println("traverse problem" + e.getMessage());
}
incrementIndegree(currentDestination);
}
}
public void traverseNeighboursDec(int currentVertex, List<Integer> destinationList) {
for (int i = 0; i < destinationList.size(); i++) {
int currentDestination = 0;
try {
currentDestination = destinationList.get(i);
System.out.println(currentDestination);
} catch (Exception e) {
System.out.println("traverse problem" + e.getMessage());
}
decreamentIndegree(currentDestination);
}
}
public void decreamentIndegree(int currentDestination){
int currentInDegree = 0;
try {
currentInDegree = indegree[currentDestination];
currentInDegree = currentInDegree - 1;
indegree[currentDestination] = currentInDegree;
} catch (IndexOutOfBoundsException e) {
System.out.println(":::" + e.getMessage());
} catch (Exception e) {
System.out.println("root exception" + e.getMessage());
}
}
public void incrementIndegree(int currentDestination){
int currentInDegree = 0;
try {
currentInDegree = indegree[currentDestination];
currentInDegree = currentInDegree + 1;
indegree[currentDestination] = currentInDegree;
} catch (IndexOutOfBoundsException e) {
System.out.println(":::" + e.getMessage());
} catch (Exception e) {
System.out.println("root exception" + e.getMessage());
}
}
public void toppologicalSort() {
int currentIndegree=0;
for (int i = 0; i < indegree.length; i++) {
if (indegree[i] == 0) {
visitedNodes[i]=1;
toppologicalSort.push(i);
List<Integer> destinationNodes = edges.get(i);
for (Integer node : destinationNodes) {
currentIndegree = indegree[node];
currentIndegree-=1;
indegree[node] = currentIndegree;
if (indegree[node] == 0) {
toppologicalSort.push(node);
visitedNodes[node]=1;
}
}
}
}
checkRemainingVisitedSources();
}
private void checkRemainingVisitedSources() {
//get all the keys from edges mean source nodes
//check if present in visited nodes.
// if not then get that vertex take its destinations if dest=0 add them also to visited.
Set<Integer> sources=edges.keySet();
boolean found=false;
for(Integer source:sources) {
if(visitedNodes[source]==0){
toppologicalSort.push(source);
traverseNeighboursDec(source,edges.get(source));
}
}
for(Integer source:sources) {
System.out.print(">>>>>>>>>>>>>>>>>>>>>>>>>>"+visitedNodes[source]);
}
}
}
public class TopologicalSort {
public static void main(String[] args){
Graph g=new Graph();
g.addEdge(5, 2);
g.addEdge(5, 0);
g.addEdge(4, 0);
g.addEdge(4, 1);
g.addEdge(2, 3);
g.addEdge(3, 1);
g.calculateMyDegree();
/*for(Integer key:g.edges.keySet())
{
System.out.println("K"+key+"value");
} */ /* for(int i=0;i<g.indegree.length;i++){
System.out.println("index"+i+"indegree"+g.indegree[i]);
}*/
g.toppologicalSort();
System.out.println(g.toppologicalSort);
System.out.println("g.indegree");
for(int i=0;i<g.indegree.length;i++){
System.out.println("i:"+i+"indegree");
System.out.println(g.indegree[i]);
}
}
}