-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindPathInGraphv1.java
More file actions
50 lines (45 loc) · 901 Bytes
/
FindPathInGraphv1.java
File metadata and controls
50 lines (45 loc) · 901 Bytes
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
package chapter9;
import java.util.*;
/*
* 给定有向图,设计一个算法,找出两个结点之间是否存在一条路径。
*/
public class FindPathInGraphv1 {
public enum State{
Unvisited,Visited,Visiting;
}
public static boolean search(Graph g,Node start,Node end)
{
LinkedList<Node> quque = new LinkedList<Node>();
for(Node u:g.getNodes())
{
u.state = State.Unvisited;
}
start.state = State.Visiting;
quque.add(start);
Node u;
while(!quque.isEmpty())
{
u = quque.removeFirst();
if(u != null)
{
for(Node v:u.getAdjacent())
{
if(v.state == State.Unvisited)
{
if(end == v)
return true;
else {
v.state = State.Visiting;
quque.add(v);
}
}
}
u.state = State.Visited;
}
}
return false;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}