FW - java - Adjacency list- 2013年12月19日15:42:04- BFS - edge Node
2013-12-20 04:41
399 查看
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(); } 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"); } } 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"); } } 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.BFS(i); } } }
runing result
this is an edge node from 0 to 1 weight is 5this is an edge node from 0 to 2 weight is 2this is an edge node from 0 to 4 weight is 15in the next linethis is an edge node from 1 to 3 weight is 1this is an edge node from 1 to 2 weight is 3in the next linethis is an edge node from 2 to 3 weight is 6this is an edge node from 2 to 0 weight is 19in the next linethis is an edge node from 3 to 4 weight is 8in the next linein the next line==== BFS RESULT ==== this is from 0this is an edge node from 0 to 1 weight is 5this is an edge node from 1 to 3 weight is 1this is an edge node from 1 to 2 weight is 3this is an edge node from 0 to 2 weight is 2this is an edge node from 0 to 4 weight is 15this is an edge node from 3 to 4 weight is 8this is an edge node from 2 to 3 weight is 6this is an edge node from 2 to 0 weight is 19this is from 1this is an edge node from 1 to 3 weight is 1this is an edge node from 3 to 4 weight is 8this is an edge node from 1 to 2 weight is 3this is an edge node from 2 to 3 weight is 6this is an edge node from 2 to 0 weight is 19this is an edge node from 0 to 1 weight is 5this is an edge node from 0 to 2 weight is 2this is an edge node from 0 to 4 weight is 15this is from 2this is an edge node from 2 to 3 weight is 6this is an edge node from 3 to 4 weight is 8this is an edge node from 2 to 0 weight is 19this is an edge node from 0 to 1 weight is 5this is an edge node from 0 to 2 weight is 2this is an edge node from 0 to 4 weight is 15this is an edge node from 1 to 3 weight is 1this is an edge node from 1 to 2 weight is 3this is from 3this is an edge node from 3 to 4 weight is 8this is from 44 is a bad point
package test_kepler;public class EdgeNode { public int fromNode; public int inNode; public int value; public boolean isVisted; public EdgeNode parentNode; public boolean isDiscovered; public EdgeNode(int fromNode,int inNode,int value) { this.fromNode = fromNode; this.inNode = inNode; this.value = value; isDiscovered = false; isVisted = false; } public void print() { System.out.println("this is an edge node from "+this.fromNode+" to "+this.inNode+" weight is "+ this.value); } // set the node's parent node; public void setParentNode(EdgeNode pareent) { this.parentNode = pareent; }};
相关文章推荐
- FW:Reverse a Singly Linked List Recursively in Java
- fw - dfs: stack/recursive ; bfs : stack/recursive- java version - 2013年12月20日18:37:18
- 黑马程序员—java技术blog—第四篇:List和Set的总结
- java中的容器类和接口总结(二):List
- 使用java.util.LinkedList模拟实现内存页面置换算法--LRU算法
- Java并发容器之CopyOnWriteArraySet与ConcurrentSkipListSet
- Java 中集合类型包含ArrayList、LinkedList、HashMap等类,下列描述正确的是(多选)?
- 【LeetCode】141 Linked List Cycle (java实现)
- java 容器类使用 Collection,Map,HashMap,hashTable,TreeMap,List,Vector,ArrayList的区别
- java 对list 排序
- java如何对list进行深度复制
- http://java.csdn.net/index.php/techlist/javalist/
- java读取csv数据到list缓存,并对list集合分组统计结果
- Java中Vector、ArrayList、List使用深入剖析
- java createSQLQuery().list()返回日期格式没有时分秒的解决方法
- Java集合源码实现二:LinkedList(jdk1.8)
- 循环双链表之Java实现(模拟JDK中LinkedList)
- java数组和list互转方法
- Java的Stack类实现List接口真的是个笑话吗
- Java基础--List的创建