最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
當(dāng)前位置: 首頁 - 科技 - 知識(shí)百科 - 正文

Crackingcodinginterview(4.2)有向圖判斷任意2點(diǎn)之間是否有一

來源:懂視網(wǎng) 責(zé)編:小采 時(shí)間:2020-11-09 08:10:57
文檔

Crackingcodinginterview(4.2)有向圖判斷任意2點(diǎn)之間是否有一

Crackingcodinginterview(4.2)有向圖判斷任意2點(diǎn)之間是否有一:4.2 Given a directed graph, design an algorithm to find out whether there is a route between two nodes. 1.圖的存儲(chǔ)使用鄰接矩陣 2.使用鄰接矩陣的過程中,必須給每個(gè)節(jié)點(diǎn)編號(hào)(0,1...) 3.可以寫一個(gè)map函數(shù)給按一定規(guī)則所有節(jié)點(diǎn)編號(hào)(本
推薦度:
導(dǎo)讀Crackingcodinginterview(4.2)有向圖判斷任意2點(diǎn)之間是否有一:4.2 Given a directed graph, design an algorithm to find out whether there is a route between two nodes. 1.圖的存儲(chǔ)使用鄰接矩陣 2.使用鄰接矩陣的過程中,必須給每個(gè)節(jié)點(diǎn)編號(hào)(0,1...) 3.可以寫一個(gè)map函數(shù)給按一定規(guī)則所有節(jié)點(diǎn)編號(hào)(本

4.2 Given a directed graph, design an algorithm to find out whether there is a route between two nodes. 1.圖的存儲(chǔ)使用鄰接矩陣 2.使用鄰接矩陣的過程中,必須給每個(gè)節(jié)點(diǎn)編號(hào)(0,1...) 3.可以寫一個(gè)map函數(shù)給按一定規(guī)則所有節(jié)點(diǎn)編號(hào)(本例未實(shí)現(xiàn)) 4.alg

4.2 Given a directed graph, design an algorithm to find out whether

there is a route between two nodes.

1.圖的存儲(chǔ)使用鄰接矩陣

2.使用鄰接矩陣的過程中,必須給每個(gè)節(jié)點(diǎn)編號(hào)(0,1...)

3.可以寫一個(gè)map函數(shù)給按一定規(guī)則所有節(jié)點(diǎn)編號(hào)(本例未實(shí)現(xiàn))

4.algorithm:通過bfs或dfs遍歷從其中一個(gè)節(jié)點(diǎn)開始遍歷圖,看是否對(duì)可達(dá)另一個(gè)節(jié)點(diǎn)。

import java.util.Queue;
import java.util.LinkedList;
import java.util.Stack;
//vertex in the graph must have serial number
//from 0 to n, if graph isn't suitable for this
//write map() to map.
//directed no-weight graph
class Graph1{
	private static boolean[][] Matrix;
	private static int vertexNum;
	public static void generator(int[][] G, int vNum){
		vertexNum = vNum;
		Matrix = new boolean[vertexNum][vertexNum];
		for(int i=0;i < G.length;i++){
			Matrix[G[i][0]][G[i][1]] = true;
		}
	}
	public static boolean isRouteDFS(int v1, int v2){
		if(v1 < 0 || v2 < 0 || 
			v1 >= Matrix.length || v2 >= Matrix.length)
			return false;
		boolean[] isVisited = new boolean[vertexNum];
		Stack s = new Stack();
		int v = -1;	
		if(v1 != v2){
			s.push(v1);
			isVisited[v1] = true;
		} else
			return true;
		while(!s.empty()){
			v = s.peek();//when v's all next node have been invisted, then pop
//			System.out.println("["+v+"]");//
			
			boolean Marked = false;	
			for(int j=0;j < Matrix[v].length;j++)
				if(Matrix[v][j] && !isVisited[j])
					if(j == v2){
						return true;
					} else{
						s.push(j);
						isVisited[j] = true;
						Marked = true;
						break;
					}
			if(!Marked)
				s.pop();
		}
		return false;
	}
	public static boolean isRouteBFS(int v1, int v2){
		if(v1 < 0 || v2 < 0 || 
			v1 >= Matrix.length || v2 >= Matrix.length)
			return false;
		boolean[] isVisited = new boolean[vertexNum];	
		Queue queue = new LinkedList();
		int v = -1;
		queue.offer(v1);
		isVisited[v1] = true;
		while(!queue.isEmpty()){
			v = queue.poll();
			if(v == v2)
				return true;
			for(int j = 0;j < Matrix[v].length;j++)
				if(Matrix[v][j] == true && isVisited[j] == false)
					queue.offer(j);
		}
		return false;
	}
	public static void printMatrix(){
		for(int i=0;i < Matrix.length;i++){
			System.out.print(i+": ");
			for(int j=0;j < Matrix[i].length;j++)
				System.out.print((Matrix[i][j] == true ? 1 : 0) + " ");
			System.out.println();
		}
	}
}
public class Solution{
	public static void main(String[] args){
		int[][] G = {
			{0, 1}, {0, 2},
			{1, 3}, {1, 4},
			{2, 4},
			{4, 0}
		};
		Graph1.generator(G, 5);
		Graph1.printMatrix();
		System.out.println("R:"+Graph1.isRouteBFS(2, 3));
		System.out.println("R:"+Graph1.isRouteDFS(2, 3));
	}
}

聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

Crackingcodinginterview(4.2)有向圖判斷任意2點(diǎn)之間是否有一

Crackingcodinginterview(4.2)有向圖判斷任意2點(diǎn)之間是否有一:4.2 Given a directed graph, design an algorithm to find out whether there is a route between two nodes. 1.圖的存儲(chǔ)使用鄰接矩陣 2.使用鄰接矩陣的過程中,必須給每個(gè)節(jié)點(diǎn)編號(hào)(0,1...) 3.可以寫一個(gè)map函數(shù)給按一定規(guī)則所有節(jié)點(diǎn)編號(hào)(本
推薦度:
標(biāo)簽: 4.2 判斷一 coding
  • 熱門焦點(diǎn)

最新推薦

猜你喜歡

熱門推薦

專題
Top