FW-4.2- decide whether the two nodes have circle - JAVA VERSION - 2013年12月20日20:22:25
2013-12-21 09:24
302 查看
Given a directed graph, design an algorithm to find out whether there is a route between two nodes.
// bfs seems easy so ; we will use dfs;
public boolean isconnected(EdgeNode e1,int dstNum)// the first node connected to the node 1 and node 2;
{
Stack<EdgeNode> isc_stack = new Stack<EdgeNode>();
isc_stack.push(e1);
while(!isc_stack.isEmpty())
{
EdgeNode head = isc_stack.pop();
if(head.isVisted == false)
{
head.print();
if(head.inNode == dstNum)
{
return true;
}
head.isVisted =true;
}
Iterator<EdgeNode> it_from = G.get(head.fromNode).iterator();
while(it_from.hasNext())
{
EdgeNode x = it_from.next();
if(x.isVisted == false)
isc_stack.push(x);
}
Iterator<EdgeNode> it_in = G.get(head.inNode).iterator();
while(it_in.hasNext())
{
EdgeNode x = it_in.next();
if(x.isVisted == false)
isc_stack.push(x);
}
}
return false;
}
// test whether from the s->e->s;
public boolean iscyccnct(int startNd,int endNd)
{
if(G.get(startNd).size() == 0 || G.get(endNd).size() ==0)
{
System.out.println("there is no line in one of the node");
return false;
}
else
{
/*
System.out.print(startNd + " ~->>~ "+ endNd);
System.out.println(isconnected(G.get(startNd).get(0), G.get(endNd).get(0)));
System.out.print(endNd + " ~->>~ "+ startNd);
System.out.println(isconnected(G.get(endNd).get(0), G.get(startNd).get(0)));
*/
boolean result = this.isconnected(G.get(startNd).get(0),endNd) && this.isconnected(G.get(endNd).get(0),startNd) ;
this.clear();
return result ;
}
}
// bfs seems easy so ; we will use dfs;
public boolean isconnected(EdgeNode e1,int dstNum)// the first node connected to the node 1 and node 2;
{
Stack<EdgeNode> isc_stack = new Stack<EdgeNode>();
isc_stack.push(e1);
while(!isc_stack.isEmpty())
{
EdgeNode head = isc_stack.pop();
if(head.isVisted == false)
{
head.print();
if(head.inNode == dstNum)
{
return true;
}
head.isVisted =true;
}
Iterator<EdgeNode> it_from = G.get(head.fromNode).iterator();
while(it_from.hasNext())
{
EdgeNode x = it_from.next();
if(x.isVisted == false)
isc_stack.push(x);
}
Iterator<EdgeNode> it_in = G.get(head.inNode).iterator();
while(it_in.hasNext())
{
EdgeNode x = it_in.next();
if(x.isVisted == false)
isc_stack.push(x);
}
}
return false;
}
// test whether from the s->e->s;
public boolean iscyccnct(int startNd,int endNd)
{
if(G.get(startNd).size() == 0 || G.get(endNd).size() ==0)
{
System.out.println("there is no line in one of the node");
return false;
}
else
{
/*
System.out.print(startNd + " ~->>~ "+ endNd);
System.out.println(isconnected(G.get(startNd).get(0), G.get(endNd).get(0)));
System.out.print(endNd + " ~->>~ "+ startNd);
System.out.println(isconnected(G.get(endNd).get(0), G.get(startNd).get(0)));
*/
boolean result = this.isconnected(G.get(startNd).get(0),endNd) && this.isconnected(G.get(endNd).get(0),startNd) ;
this.clear();
return result ;
}
}
相关文章推荐
- fw 3.5 -2013年12月16日21:02:12 - two stacks make a queue- java version -
- fw - dfs: stack/recursive ; bfs : stack/recursive- java version - 2013年12月20日18:37:18
- File Comparer - To compare two files and check whether they have the same content
- java.sql.SQLException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for
- Java入门到精通——调错篇之Eclipse Java compiler level dose not match the version of the installed Java project
- 136.You have two database servers SEMP and SACCT. The database in the SEMP server maintains the empl
- You are attempting to build with the incorrect version of java
- 百度地图API报错 Caused by: java.lang.IllegalStateException: you have not supplyed the global app context i
- Description Resource Path Location Type Java compiler level does not match the version of the instal
- Java compiler level does not match the version of the installed Java project facet.问题解决办法
- LeetCode461 Hamming Distance java The Hamming distance between two integers is the number osoluotion
- 解决java compiler level does not match the version of the installed java project facet
- eclipse4.2碰到的failed to create the java virtual machine
- 解决java compiler level does not match the version of the installed java project facet .
- Java compiler level does not match the version of the installed Java project facet
- 解决java compiler level does not match the version of the installed java project facet
- Java compiler level does not match the version of the installed Java project facet 的解决方案
- maven -- 问题解决(三)Java compiler level does not match the version of the installed Java project facet
- Java compiler level does not match the version of the installed Java project facet
- eclipse提示java compiler level does not match the version of the installed java