最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501
当前位置: 首页 - 科技 - 知识百科 - 正文

Crackingcodinginterview(4.2)有向图判断任意2点之间是否有一

来源:懂视网 责编:小采 时间:2020-11-09 08:10:57
文档

Crackingcodinginterview(4.2)有向图判断任意2点之间是否有一

Crackingcodinginterview(4.2)有向图判断任意2点之间是否有一:4.2 Given a directed graph, design an algorithm to find out whether there is a route between two nodes. 1.图的存储使用邻接矩阵 2.使用邻接矩阵的过程中,必须给每个节点编号(0,1...) 3.可以写一个map函数给按一定规则所有节点编号(本
推荐度:
导读Crackingcodinginterview(4.2)有向图判断任意2点之间是否有一:4.2 Given a directed graph, design an algorithm to find out whether there is a route between two nodes. 1.图的存储使用邻接矩阵 2.使用邻接矩阵的过程中,必须给每个节点编号(0,1...) 3.可以写一个map函数给按一定规则所有节点编号(本

4.2 Given a directed graph, design an algorithm to find out whether there is a route between two nodes. 1.图的存储使用邻接矩阵 2.使用邻接矩阵的过程中,必须给每个节点编号(0,1...) 3.可以写一个map函数给按一定规则所有节点编号(本例未实现) 4.alg

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

there is a route between two nodes.

1.图的存储使用邻接矩阵

2.使用邻接矩阵的过程中,必须给每个节点编号(0,1...)

3.可以写一个map函数给按一定规则所有节点编号(本例未实现)

4.algorithm:通过bfs或dfs遍历从其中一个节点开始遍历图,看是否对可达另一个节点。

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));
	}
}

声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文档

Crackingcodinginterview(4.2)有向图判断任意2点之间是否有一

Crackingcodinginterview(4.2)有向图判断任意2点之间是否有一:4.2 Given a directed graph, design an algorithm to find out whether there is a route between two nodes. 1.图的存储使用邻接矩阵 2.使用邻接矩阵的过程中,必须给每个节点编号(0,1...) 3.可以写一个map函数给按一定规则所有节点编号(本
推荐度:
标签: 4.2 判断一 coding
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top