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
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作为遍历的起点,而且需要输入顶点的个数(虽然没用)。