您的位置:首页 > Web前端 > Node.js

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;	}};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: