Skip to content

Commit 1e7a932

Browse files
committed
simplified DFS, topological sorting
1 parent 8f2ae44 commit 1e7a932

File tree

6 files changed

+83
-192
lines changed

6 files changed

+83
-192
lines changed

src/algo/graph/BFS.java

+14-17
Original file line numberDiff line numberDiff line change
@@ -13,49 +13,46 @@
1313
*/
1414
public class BFS {
1515

16-
Map<Integer, Integer> path = new HashMap<>();
16+
List<Integer> path = new LinkedList<>();
1717
private Graph graph;
1818

19+
1920
public BFS(Graph graph) {
2021
this.graph = graph;
2122
}
2223

2324
public void search(Integer root) {
2425
if (root == null || !graph.getVertices().contains(root)) return;
26+
2527
Set<Integer> visited = new HashSet<>();
2628
Queue<Integer> queue = new LinkedList<>();
29+
2730
visited.add(root);
2831
queue.add(root);
29-
path.put(root, null);
30-
while (!queue.isEmpty()){
32+
33+
34+
while (!queue.isEmpty()) {
35+
3136
Integer vertex = queue.remove();
3237
proccessVertex(vertex);
38+
3339
for (Integer neighbor : graph.getNeighbors(vertex))
3440
if (!visited.contains(neighbor)) {
35-
path.put(neighbor, vertex);
3641
visited.add(neighbor);
3742
queue.add(neighbor);
3843
}
3944
}
4045
}
4146

4247
private void proccessVertex(Integer vertex) {
43-
// process vertex: default not nothing;
48+
path.add(vertex);
4449
}
4550

4651

47-
public List<Integer> getPathFrom(Integer source, Integer sink) {
48-
List<Integer> list = new ArrayList<>();
49-
if (sink == null || !graph.getVertices().contains(sink))
50-
return list;
51-
52+
public List<Integer> getPathFrom(Integer source) {
53+
if (source == null || !graph.getVertices().contains(source))
54+
return null;
5255
search(source);
53-
54-
Integer current = sink;
55-
while (path.get(current) != null) {
56-
list.add(current);
57-
current = path.get(current);
58-
}
59-
return list;
56+
return path;
6057
}
6158
}

src/algo/graph/BFSBottomUp.java

-51
This file was deleted.

src/algo/graph/DFS.java

+44-37
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,68 @@
11
package algo.graph;
22

33
import ds.graph.Graph;
4-
import ds.graph.Vertex;
54

6-
import java.util.Stack;
5+
import java.util.*;
76

87
/**
98
* Created by sherxon on 1/4/17.
109
*/
11-
public class DFS<T> {
12-
Graph<T, Number> graph;
10+
public class DFS {
11+
protected Graph graph;
12+
Set<Integer> visited;
13+
List<Integer> path;
1314

14-
public DFS(Graph<T, Number> graph) {
15+
public DFS(Graph graph) {
1516
this.graph = graph;
17+
visited = new HashSet<>();
18+
path = new LinkedList<>();
1619
}
1720

18-
public void search(T source) {
19-
Vertex<T> vertex= graph.getVertex(source);
20-
vertex.setVisited(true);
21-
for (Vertex<T> tVertex : vertex.getNeighbors())
22-
if(!tVertex.isVisited()){
23-
tVertex.setParent(vertex);
24-
search(tVertex.getValue());
21+
public void search(Integer source) {
22+
23+
visited.add(source);
24+
processVertex(source);
25+
26+
for (Integer neighbor : graph.getNeighbors(source))
27+
if (!visited.contains(neighbor)) {
28+
search(neighbor);
2529
}
2630
}
27-
public void searchIterative(T source) {
28-
Vertex<T> vertex= graph.getVertex(source);
29-
Stack<Vertex<T>> stack= new Stack<>();
30-
stack.add(vertex);
31+
32+
public void processVertex(Integer source) {
33+
path.add(source);
34+
}
35+
36+
public void searchIterative(Integer source) {
37+
if (source == null || !graph.getVertices().contains(source)) return;
38+
39+
visited.clear();
40+
path.clear();
41+
42+
Stack<Integer> stack = new Stack<>();
43+
stack.add(source);
44+
3145
while (!stack.isEmpty()){
32-
Vertex<T> v=stack.pop();
33-
v.setVisited(true);
34-
for (Vertex<T> tVertex : v.getNeighbors()) {
35-
if(!tVertex.isVisited()){
36-
tVertex.setParent(v);
37-
stack.add(tVertex);
38-
}
46+
Integer v = stack.pop();
47+
48+
visited.add(v);
49+
processVertex(v);
50+
51+
for (Integer neighbor : graph.getNeighbors(v)) {
52+
if (!visited.contains(neighbor))
53+
stack.add(neighbor);
3954
}
4055
}
4156

4257
}
4358

4459

45-
private void clearGraph() {
46-
for (Vertex<T> vertex : graph.getVertices()) {
47-
vertex.setVisited(false);
48-
vertex.setParent(null);
49-
}
50-
}
51-
public void printPathFrom(T t){
52-
Vertex<T> root=graph.getVertex(t);
53-
if(root==null)return;
54-
while (root.getParent()!=null){
55-
System.out.print(root.getValue() + " -> ");
56-
root=root.getParent();
57-
}
58-
System.out.println("a");
60+
public List<Integer> getPathFrom(Integer source) {
61+
62+
if (source == null || !graph.getVertices().contains(source))
63+
return null;
64+
search(source);
65+
return path;
5966
}
6067

6168

src/algo/graph/Test.java

-63
This file was deleted.
+20-24
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,46 @@
11
package algo.graph;
22

33
import ds.graph.Graph;
4-
import ds.graph.Vertex;
54

5+
import java.util.ArrayList;
66
import java.util.HashSet;
77
import java.util.List;
88
import java.util.Set;
9-
import java.util.Stack;
109

1110
/**
1211
* Created by sherxon on 1/4/17.
1312
*/
14-
public class TopologicalSorting<T> {
15-
private Graph<T, Number> graph;
13+
public class TopologicalSorting extends DFS {
14+
List<Integer> list;
1615

17-
public TopologicalSorting(Graph<T, Number> graph) {
18-
this.graph = graph;
16+
public TopologicalSorting(Graph graph) {
17+
super(graph);
18+
list = new ArrayList<>();
1919
}
2020

2121
// this works with DAG Only
2222
// first we will choose any vertex who that does not have incoming edges (sources)
2323
// sources can be found easier if incoming edge count is recorded in each vertex
24-
List<T> topSort(){
25-
Stack<T> stack=new Stack<>();//stack is also good option
26-
Set<Vertex<T>> sources=new HashSet<>();
27-
for (Vertex<T> vertex : graph.getVertices())
24+
List<Integer> topSort() {
25+
26+
Set<Integer> sources = new HashSet<>();
27+
for (Integer vertex : graph.getVertices())
2828
sources.add(vertex);
2929

30-
for (Vertex<T> vertex : graph.getVertices())
31-
for (Vertex<T> tVertex : vertex.getNeighbors())
30+
for (Integer vertex : graph.getVertices())
31+
for (Integer tVertex : graph.getNeighbors(vertex))
3232
sources.remove(tVertex);
3333

34-
for (Vertex<T> source : sources)
35-
if(!source.isVisited())
36-
dfs(source, stack);
34+
for (Integer source : sources)
35+
if (!visited.contains(source))
36+
search(source);
3737

38-
return stack;
38+
return list;
3939
}
4040

41-
private void dfs(Vertex<T> source, List<T> list) {
42-
source.setVisited(true);
43-
for (Vertex<T> vertex : source.getNeighbors()) {
44-
if(!vertex.isVisited()){
45-
dfs(vertex, list);
46-
}
47-
}
48-
list.add(source.getValue());
41+
@Override
42+
public void processVertex(Integer source) {
43+
super.processVertex(source);
44+
list.add(source);
4945
}
5046
}

src/ds/graph/UndirectedGraph.java

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ public Set<Integer> getVertices() {
2424
return new HashSet<>(vertexMap.keySet());
2525
}
2626

27+
@Override
28+
public Set<Integer> getNeighbors(Integer ver) {
29+
return vertexMap.get(ver);
30+
}
31+
2732

2833
@Override
2934
public Double addEdge(Integer v1, Integer v2) {

0 commit comments

Comments
 (0)