fw - dfs: stack/recursive ; bfs : stack/recursive- java version - 2013年12月20日18:37:18
2013-12-21 07:36
387 查看
package test_kepler; import java.util.*; import test_kepler.Treeandgraph.Node; public class MyGraph { //In this class we assume we focus on the node in a graph; ArrayList<ArrayList<EdgeNode>> G = new ArrayList<ArrayList<EdgeNode>>(); int nodeNumber; public MyGraph(int nodeNm) { nodeNumber=nodeNm; for(int i = 0;i<nodeNumber;++i) { G.add(new ArrayList<EdgeNode>()); } } public void set(int fromNode,int inNode,int edgeValue) { //fromNode--; //inNode--; //edgeValue--; EdgeNode newedgeNode = null; if(fromNode<nodeNumber && inNode < nodeNumber) { newedgeNode = new EdgeNode(fromNode,inNode,edgeValue); G.get(fromNode).add(newedgeNode); } else { System.out.println("set number is wrong"); } } public void MakeGraph() { set(0,1,5); set(1,3,1); set(1,2,3); set(0,2,2); set(2,3,6); set(3,4,8); set(0,4,15); set(2,0,19); } public ArrayList<EdgeNode> getNeighbor(int i) { if(i<nodeNumber) { return G.get(i); } else { System.out.println("query wrong"); return null; } } public void BFS(int startNode) { qbytwostc testq = new qbytwostc<EdgeNode>(); //start from 1; //EdgeNode dnd = G.get(0).get(0);//perfect point ~~~ System.out.println("this is from "+ startNode); if(G.get(startNode).size() == 0) { System.out.println(startNode+" is a bad point "); return ; } EdgeNode dnd = G.get(startNode).get(0); // dnd.print(); testq.enQueue(dnd); // System.out.println("after enqueue"); while(!testq.isEmpty()) { EdgeNode headfrmQueue = (EdgeNode) testq.dequeue(); if(headfrmQueue.isVisted == false) { headfrmQueue.isVisted = true; headfrmQueue.print(); } Iterator<EdgeNode> it = G.get(headfrmQueue.inNode).iterator(); while(it.hasNext()) { EdgeNode x = it.next(); if(x.isVisted == false) { x.isDiscovered = true; testq.enQueue(x); } } Iterator<EdgeNode> it2 = G.get(headfrmQueue.fromNode).iterator(); while(it2.hasNext()) { // System.out.println("2??"); EdgeNode x2 = it2.next(); if(x2.isVisted == false)// && x2.isDiscovered == false) { x2.isDiscovered = true; testq.enQueue(x2); } } } clear(); } // dfs - recursive public void DFS(EdgeNode startNode) { if(startNode.isVisted == false) { startNode.print(); startNode.isVisted = true; } Iterator<EdgeNode> it = G.get(startNode.inNode).iterator(); while(it.hasNext()) { EdgeNode x = it.next(); if(x.isVisted == false) { DFS(x); } } Iterator<EdgeNode> it2 = G.get(startNode.fromNode).iterator(); while(it2.hasNext()) { // System.out.println("2??"); EdgeNode x2 = it2.next(); if(x2.isVisted == false)// && x2.isDiscovered == false) { this.DFS(x2); } } } // wrap for DFS public void DFS_wrap(int startNode) { if(G.get(startNode).size() == 0) { System.out.println(startNode+" is a bad point "); return ; } EdgeNode BGNd = G.get(startNode).get(0); // dnd.print(); System.out.println("this is from" + startNode+"===="); // DFS(BGNd); DFS_Estack(BGNd); this.clear(); } public void print() { for(int index = 0;index<nodeNumber;++index) { Iterator<EdgeNode> it = G.get(index).iterator(); while(it.hasNext()) { EdgeNode x = it.next(); x.print(); } System.out.println("in the next line"); } } // CLEAR ALL THE node as un-visited void clear() { for(int index = 0;index<nodeNumber;++index) { Iterator<EdgeNode> it = G.get(index).iterator(); while(it.hasNext()) { EdgeNode x = it.next(); x.isVisted = false; x.isDiscovered = false; } //System.out.println("in the next line"); } } // bfs seems easy so ; we will use dfs; boolean isconnected(EdgeNode e1,EdgeNode e2)// the first node connected to the node 1 and node 2; { return false; } // test whether from the s->e->s; public boolean iscyccnct(int startNd,int endNd) { return false; } // dfs with explicit stack // the call the function itself is a kind of push stack; // it can even get back ~~~ because the stack will pop to the ; changing-to-another-place across corneer `` public void DFS_Estack(EdgeNode startNd1) { Stack<EdgeNode> dfs_stack = new Stack<EdgeNode>(); dfs_stack.push(startNd1); while(!dfs_stack.isEmpty()) { EdgeNode startNd = dfs_stack.pop(); if(startNd.isVisted == false) { startNd.print(); startNd.isVisted = true; } Iterator<EdgeNode> it = G.get(startNd.fromNode).iterator(); while(it.hasNext()) { EdgeNode newNode = it.next(); if(newNode.isVisted == false) dfs_stack.push(newNode); } Iterator<EdgeNode> it2 = G.get(startNd.inNode).iterator(); while(it2.hasNext()) { EdgeNode newNode = it2.next(); if(newNode.isVisted == false) dfs_stack.push(newNode); } } } public static void main(String[] args) { // TODO Auto-generated method stub MyGraph mg= new MyGraph(5); mg.MakeGraph(); mg.print(); System.out.println("==== BFS RESULT ==== "); //mg.BFS(4); for(int i = 0;i<5;++i) { mg.DFS_wrap(i); } } }
相关文章推荐
- fw 3.6 - stack sort- java version - 2013年12月16日23:23:32
- FW-4.2- decide whether the two nodes have circle - JAVA VERSION - 2013年12月20日20:22:25
- FWNX-4,.5 JAVA VERSION - SUCCESS OF IN_ORDER_TRAVERSE
- BFS和DFS详解以及java实现
- fw -BFS & DFS tree-2-013年12月17日17:56:33
- 图的BFS和DFS原理及实例分析(java)
- java.io.FileNotFoundException: /home/hadoop/hadoop/dfs/namenode/current/VERSION (Permission denied)
- BFS和DFS的java实现
- BFS和DFS详解以及java实现
- java实现邻接表图、DFS、BFS
- 搜索算法(一)--DFS/BFS求解拯救同伴问题(JAVA)
- fw12013年12月13日1:17:54 - multi-stack -java
- 【leetcode】104. Maximum Depth of Binary Tree【java】三种实现方法:递归、BFS、DFS
- DFS深度优先搜索算法与BFS广度优先搜索算法的java实现
- java版 图的邻接表、邻接矩阵、BFS、DFS 实现
- 复习一下dfs和bfs 用java
- Java实现算法导论中图的广度优先搜索(BFS)和深度优先搜索(DFS)
- Graph Traversal (BFS vs DFS vs Stack)
- 搜索算法(二)--DFS/BFS求解炸弹人问题(JAVA )
- BFS和DFS的java实现