forked from BaseMax/DepthFirstSearchJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDepthFirstSearch.java
More file actions
141 lines (121 loc) · 3.36 KB
/
DepthFirstSearch.java
File metadata and controls
141 lines (121 loc) · 3.36 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
/**
*
* @name: Depth First Search
* @author: Max Base
* @repository: https://github.com/BaseMax/DepthFirstSearchJava
* @date: 2022/12/19
*
*/
class StackX {
private final int SIZE = 20;
private int[] st;
private int top;
public StackX()
{
st = new int[SIZE];
top = -1;
}
public void push(int j)
{
st[++top] = j;
}
public int pop()
{
return st[top--];
}
public int peek()
{
return st[top];
}
public boolean isEmpty()
{
return (top == -1);
}
}
class Vertex {
public char label;
public boolean wasVisited;
public Vertex(char lab)
{
label = lab;
wasVisited = false;
}
}
class Graph {
private final int MAX_VERTS = 20;
private Vertex vertexList[]; // List of vertices
private int adjMat[][]; // Adjacency matrix
private int nVerts; // Current number of vertices
private StackX theStack;
public Graph()
{
vertexList = new Vertex[MAX_VERTS];
// adjacency matrix
adjMat = new int[MAX_VERTS][MAX_VERTS];
nVerts = 0;
for (int j = 0; j < MAX_VERTS; j++) // Set adjacency
for (int k = 0; k < MAX_VERTS; k++) // Matrix to 0
adjMat[j][k] = 0;
theStack = new StackX();
}
// Add vertex to vertex list
public void addVertex(char lab)
{
vertexList[nVerts++] = new Vertex(lab);
}
// Add edge to edge matrix
public void addEdge(int start, int end)
{
adjMat[start][end] = 1;
adjMat[end][start] = 1;
}
// Display vertex
public void displayVertex(int v)
{
System.out.print(vertexList[v].label);
}
// Depth-first search
public void dfs()
{
if (nVerts == 0) {
System.out.println("No vertices in graph");
return;
}
vertexList[0].wasVisited = true; // mark it
displayVertex(0); // display it
theStack.push(0); // push it
// Until stack empty,
while (!theStack.isEmpty()) {
// Get an unvisited vertex adjacent to stack top
int v = getAdjUnvisitedVertex(theStack.peek());
if (v == -1) // if no such vertex,
theStack.pop();
else { // if it exists
vertexList[v].wasVisited = true; // mark it
displayVertex(v); // display it
theStack.push(v); // push it
}
}
// Stack is empty, so we're done
for (int j = 0; j < nVerts; j++) // reset flags
vertexList[j].wasVisited = false;
}
}
public class DepthFirstSearch {
public static void main(String[] args)
{
Graph theGraph = new Graph();
theGraph.addVertex('A'); // 0 (start for dfs)
theGraph.addVertex('B'); // 1
theGraph.addVertex('C'); // 2
theGraph.addVertex('D'); // 3
theGraph.addVertex('E'); // 4
theGraph.addEdge(0, 1); // AB
theGraph.addEdge(1, 2); // BC
theGraph.addEdge(0, 3); // AD
theGraph.addEdge(3, 4); // DE
System.out.print("Visits: ");
theGraph.dfs(); // depth-first search
System.out.println();
}
}