【数据结构】【图论】DFS

      DFS(Depth-First-Search),中文名称为“深度优先搜索”,是纵向遍历图的一种递归算法。

      需求:输入edgeCount、startNode,然后输入edgeCount组数据,每组数据有两个数node1与node2,表示一条无向边,最后使用DFS算法输出遍历图的结果,数和数之间使用空格隔开。

一、Java代码

import java.util.Scanner;
class Node{
	int nodeValue;
	Node next=null;
}
public class DFS{
	private static boolean[] visited=new boolean[1024];
	private static Node[] nodes=new Node[1024];
	public static void main(String args[]){
		init();
		Scanner scanner=new Scanner(System.in);		
		int edgeCount=scanner.nextInt();
		int startNode=scanner.nextInt();
		for(int i=0;i<edgeCount;i++){
			int nodeValue1=scanner.nextInt();
			int nodeValue2=scanner.nextInt();
			
			Node node1=new Node();
			node1.nodeValue=nodeValue2;
			node1.next=nodes[nodeValue1].next;
			nodes[nodeValue1].next=node1;	

			Node node2=new Node();
			node2.nodeValue=nodeValue1;
			node2.next=nodes[nodeValue2].next;
			nodes[nodeValue2].next=node2;
		}	
		dfs(nodes,visited,startNode);
		System.out.println();
	}
	public static void dfs(Node[] nodes,boolean[] visited,int startNode){
		Node node=nodes[startNode].next;
		visited[startNode]=true;
		System.out.print(startNode+" ");
		sort(node);
		while(node!=null){
			if(visited[node.nodeValue]==false){
				dfs(nodes,visited,node.nodeValue);
			}
			node=node.next;
		}		
	}
	public static void sort(Node node){
		boolean flag=false;
		while(flag=!flag){
			Node p=node;
			Node q=p.next;
			while(q!=null){
				if(p.nodeValue>q.nodeValue){
					int temp=p.nodeValue;
					p.nodeValue=q.nodeValue;
					q.nodeValue=temp;
					flag=false;
				}else{
					p=p.next;
					q=p.next;
				}
			}				
		}
		
	}
	public static void init(){
		for(int i=0;i<visited.length;i++){
			visited[i]=false;
		}
		for(int i=0;i<nodes.length;i++){
			nodes[i]=new Node();
		}
	}
}

    上述代码中对邻接表进行了排序,所以边的输入顺序不会影响到最终的结果。

二、测试数据

输入
7 0 0 3 0 4 1 4 1 5 2 3 2 4 3 5
输出
0 3 2 4 1 5

 三、ACM

    题目链接:图的深度遍历

    AC代码(Java版):

import java.util.Scanner;
class Node{
	int nodeValue;
	Node next=null;
}
public class Main{
	private static boolean[] visited=new boolean[1024];
	private static Node[] nodes=new Node[1024];
	private static boolean isFirst=false;
	public static void main(String args[]){
		Scanner scanner=new Scanner(System.in);		
		int count=scanner.nextInt();
		while((count--)!=0){
			init();
			int nodeCount=scanner.nextInt();
			int edgeCount=scanner.nextInt();
			int startNode=0;
			for(int i=0;i<edgeCount;i++){
				int nodeValue1=scanner.nextInt();
				int nodeValue2=scanner.nextInt();
			
				Node node1=new Node();
				node1.nodeValue=nodeValue2;
				node1.next=nodes[nodeValue1].next;
				nodes[nodeValue1].next=node1;	

				Node node2=new Node();
				node2.nodeValue=nodeValue1;
				node2.next=nodes[nodeValue2].next;
				nodes[nodeValue2].next=node2;
			}	
			dfs(nodes,visited,startNode);
			System.out.println();
		}
	}
	public static void dfs(Node[] nodes,boolean[] visited,int startNode){
		Node node=nodes[startNode].next;
		visited[startNode]=true;
		if(!isFirst){
			System.out.print(startNode);
			isFirst=true;
		}else{
			System.out.print(" "+startNode);
		}
		sort(node);
		while(node!=null){
			if(visited[node.nodeValue]==false){
				dfs(nodes,visited,node.nodeValue);
			}
			node=node.next;
		}		
	}
	public static void sort(Node node){
		boolean flag=false;
		while(flag=!flag){
			Node p=node;
			Node q=p.next;
			while(q!=null){
				if(p.nodeValue>q.nodeValue){
					int temp=p.nodeValue;
					p.nodeValue=q.nodeValue;
					q.nodeValue=temp;
					flag=false;
				}else{
					p=p.next;
					q=p.next;
				}
			}				
		}
		
	}
	public static void init(){
		isFirst=false;
		for(int i=0;i<visited.length;i++){
			visited[i]=false;
		}
		for(int i=0;i<nodes.length;i++){
			nodes[i]=new Node();
		}
	}
}

    需要注意的是这里默认使用0作为遍历的起点,而且需要输入顶点的个数(虽然没用)。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值