-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.java
More file actions
55 lines (51 loc) · 1.3 KB
/
Copy pathGraph.java
File metadata and controls
55 lines (51 loc) · 1.3 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
import java.util.*;
class Graph1{
class Edge{
String Source,Destination;
public Edge(String Source,String Destination){
this.Source=Source; this.Destination=Destination;
}
@Override
public String toString(){
return "\n" +Source +"=>"+Destination;
}
}
List<Edge> G[];
public Graph1(int n){
G=new LinkedList[n];
for(int i=0;i<G.length;i++)
G[i]=new LinkedList<Edge>();
}
boolean isConnected(String Source,String Destination){
for(Edge source: G[1])
if(source.Destination==Destination)
return true;
return false;
}
void addEdge(String Source,String Destination){
for(int i=0;i<G.length;i++) {
G[i].add(0,new Edge(Source,Destination));
}
}
@Override
public String toString(){
String result="";
for(int i=0;i<G.length;i++)
result=G[i]+"\n";
return result;
}
}
public class Graph {
public static void main(String[] args) {
Graph1 g=new Graph1(7);
g.addEdge("Website A", "Website B");
g.addEdge("Website A", "Website C");
g.addEdge("Website B", "Website C");
g.addEdge("Website B", "Website E");
g.addEdge("Website C", "Website B");
g.addEdge("Website C", "Website F");
g.addEdge("Website E", "Website B");
System.out.println(g);
System.out.println(g.isConnected("Website A","Website C"));
}
}