4.2

Topic:Given a directed graph, design an algorithm to find out whether there is a route between two nodes.

// 方法:Do graph traversal, DFS or BFS. Start from one node, and check if the other can be found.

// 关键:1) Mark visited nodes to avoid cycles.

2) DFS is easy, just do simple recursion. But it may go very deep before go to its immediate neighbor.

public class Vertice {
	private Vertice adjacent[];// Vertice是一个类,它的属性有name, count, 以及adjacent[]的数组(数组元素的类型是Vertice
    public int adjacentCount;
    private String name;
    public C.State state;//注意这里
    
    
    public Vertice(String name, int adjacentLength) {
        this.name = name;                
        adjacentCount = 0;        
        adjacent = new Vertice[adjacentLength];
    }
    
    public void addAdjacent(Vertice x) {
            this.adjacent[adjacentCount] = x;
            adjacentCount++;// 这里可能x的数量超过了邻接矩阵的长度,要注意 可以设一个条件,比如if(adjacentCount<30), else, print"no more than"
}
    
    public Vertice[] getAdjacent() {
        return adjacent;
    }
    public String getVertex() {
        return name;
    }
}
public class Graph {
	private Vertice graph[]; //Graph是一个类,其属性有size, 及元素为结点的数组
	public int count;
	
	public Graph(){
		graph=new Vertice[6];//数组大小为6,元素类型为Vertice, 名字为graph
		count=0;
	}
	
	 public void addVertice(Vertice x) {
		 graph[count]=x;//简单
		 count++;
	 }
	 
	 public Vertice[] getVertices(){
		 return graph;
	 }
}
import java.util.LinkedList;
public class C {
	public enum State {
		Unvisited, Visited, Visiting;}
	
	public static Graph createNewGraph()
	{
		Graph g = new Graph();        
		Vertice[] temp = new Vertice[6];

		temp[0] = new Vertice("a", 3);
		temp[1] = new Vertice("b", 0);
		temp[2] = new Vertice("c", 0);
		temp[3] = new Vertice("d", 1);
		temp[4] = new Vertice("e", 1);
		temp[5] = new Vertice("f", 0);

		temp[0].addAdjacent(temp[1]);
		temp[0].addAdjacent(temp[2]);
		temp[0].addAdjacent(temp[3]);
		temp[3].addAdjacent(temp[4]);
		temp[4].addAdjacent(temp[5]);
		for (int i = 0; i < 6; i++) {
			g.addVertice(temp[i]);
		}
		return g;
	}

	public static boolean BFS(Graph g, Vertice start, Vertice end) {  
		for (Vertice u : g.getVertices()) {
            u.state = State.Unvisited;// 有点意思
        }
        start.state = State.Visiting;
        //需要建一个队列来存储正在visiting的元素,直到所有都是visited 用LinkedList来模拟
        LinkedList<Vertice> queue=new LinkedList<Vertice>();
        queue.add(start);
        Vertice u;
        while(!queue.isEmpty()){//队列里没元素了,所有结点就都访问过了
        	u=queue.removeFirst();//依次从队列的头开始
        	if(u!=null){
        		for (Vertice v:u.getAdjacent()){
        			if(v.state==State.Unvisited){//访问一个节点,找到,结束,没找到,放到Visiting队列
        				if(v==end) {
        					return true;
        				}
        				else {
        					v.state=State.Visiting;//Visiting这个状态也可以不要,但留着比较直观
        					queue.add(v);
        				}
        			}
        		}
        		u.state=State.Visited;
        	}
        }
        return false;
	}
	
	public static void main(String a[])
	{
		Graph g = createNewGraph();
		Vertice[] n = g.getVertices();
		Vertice start = n[1];
		Vertice end = n[5];
		System.out.println(BFS(g, start, end));
	}
}
//结果
false





 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值