-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDirectPath.java
More file actions
70 lines (59 loc) · 1.32 KB
/
DirectPath.java
File metadata and controls
70 lines (59 loc) · 1.32 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
package edu.java.chap4;
import java.util.LinkedList;
//given a directed graph, design an algorithm to find out whether there is a route between two nodes.
/*
*/
public class DirectPath {
//public static void DFS(Node root){
//}
public static boolean CheckReach(int from, int to, int[][] vertice){
if(from == to) return true;
LinkedList<Integer> list = new LinkedList<Integer> ();
list.add(from);
int pos = 0;
while(list.size()>pos){
from = list.get(pos);
for(int i = 0; i<vertice.length;i++){
if(vertice[i][0] == from &&!check(list,vertice[i][1])){
list.add(vertice[i][1]);
}
}
if(check(list,to)){
return true;
}
pos++;
}
return false;
}
public static boolean check(LinkedList<Integer> list, int data){
for(int i = 0; i<list.size();i++){
if(list.get(i) ==data){
return true;
}
}
return false;
}
public static void main(String[] args) {
// build graph
/*Node n1 = new Node(1);
Node n2 = new Node(3);
Node n3 = new Node(7);
Node n4 = new Node(4);
Node n5 = new Node(9);
*/
int[][] vertice = {{1,2},{2,1},{1,5},{3,2},{2,4},{5,4}};
for(int i = 1; i<6;i++){
System.out.println(CheckReach(1,i,vertice));
}
//check reach
}
}
/*
class Node{
Object data;
Node to = null;
Node from = null;
Node(Object data){
this.data = data;
}
}*/