Skip to content

Commit 9987436

Browse files
committed
07.01 (1) BFS topological sorting
1 parent 13611ab commit 9987436

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package _01_TopologicalSorting;
2+
3+
import java.util.ArrayList;
4+
5+
public class DirectedGraphNode {
6+
7+
int label;
8+
9+
ArrayList<DirectedGraphNode> neighbors;
10+
11+
public DirectedGraphNode(int x) {
12+
label = x;
13+
neighbors = new ArrayList<>();
14+
}
15+
16+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* Time : O() ; Space: O()
3+
* @tag : LintCode Copyright; Geeks for Geeks; Depth First Search; Breadth First Search
4+
* @by : Steven Cooks
5+
* @date: Sep 26, 2015
6+
***************************************************************************
7+
* Description:
8+
*
9+
* Given an directed graph, a topological order of the graph nodes is defined
10+
* as follow:
11+
*
12+
* For each directed edge A -> B in graph, A must before B in the order list.
13+
* The first node in the order can be any node in the graph with no nodes direct to it.
14+
* Find any topological order for the given graph.
15+
*
16+
***************************************************************************
17+
* {@link http://www.lintcode.com/en/problem/topological-sorting/ }
18+
*/
19+
package _01_TopologicalSorting;
20+
21+
import java.util.ArrayList;
22+
import java.util.Hashtable;
23+
import java.util.LinkedList;
24+
import java.util.Map;
25+
import java.util.Queue;
26+
27+
/** see test {@link _01_TopologicalSorting.SolutionTest } */
28+
public class Solution {
29+
30+
public ArrayList<DirectedGraphNode> topSort(ArrayList<DirectedGraphNode> graph) {
31+
Map<DirectedGraphNode, Integer> in = new Hashtable<>();
32+
// count in-degree for each node in the graph
33+
for (DirectedGraphNode node : graph) {
34+
if (!in.containsKey(node)) {
35+
in.put(node, 0);
36+
}
37+
for (DirectedGraphNode neighbor : node.neighbors) {
38+
int degree = 1;
39+
if (in.containsKey(neighbor)) {
40+
degree += in.get(neighbor);
41+
}
42+
in.put(neighbor, degree);
43+
}
44+
}
45+
46+
// put all nodes with zero in-degree
47+
Queue<DirectedGraphNode> zeros = new LinkedList<>();
48+
for (DirectedGraphNode node : in.keySet()) {
49+
if (in.get(node) == 0) {
50+
zeros.offer(node);
51+
}
52+
}
53+
54+
// construct result
55+
ArrayList<DirectedGraphNode> res = new ArrayList<>();
56+
while (!zeros.isEmpty()) {
57+
DirectedGraphNode node = zeros.poll();
58+
res.add(node);
59+
for (DirectedGraphNode neighbor : node.neighbors) {
60+
int degree = in.get(neighbor) - 1;
61+
if (degree > 0) {
62+
in.put(neighbor, degree);
63+
}
64+
if (degree == 0) {
65+
zeros.offer(neighbor);
66+
}
67+
}
68+
}
69+
return res;
70+
}
71+
72+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package _01_TopologicalSorting;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.Test;
6+
7+
public class SolutionTest {
8+
9+
@Test
10+
public void test() {
11+
fail("Not yet implemented"); // TODO
12+
}
13+
14+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy