Topological sort (FIG adjacent table storage)

Topological sort is to have all the vertices of G to FIG arranged in a linear sequence, such that for any two vertices u graph G, if there is an edge u v, v u then the sequence must be in front of v, this sequences is referred to as a topological sequence

Therefore, there is a topological sort is directed to FIG, to be used to determine whether there is a ring with a directed graph (depth-first search may be determined whether there is a ring with a directed graph)

2. performing a topological sort process is as follows:

① define a queue, and all of the node 0 is added to the queue to

Remove the first team node ②, the output, and then delete all edges emanating from it, and so that the vertices of these edges arrives minus 1, determining the degree of a vertex is 0 if 0 is then added to the queue

③ ② operation is repeatedly executed until the queue is empty, if the number of the queue is empty node pair is exactly the N, topological sort described topological sorting success or failure, is present in the ring of FIG.

3. Use the following Java programming language to complete the process:

① first need to solve the problem of storage of the map, where we choose to use the adjacent table to store map, you can declare a set of arrays, each array element is a collection, then you can imitate the list created by the array the subscripts indicate the number of vertices

② Because of the nodes need to record, so it is necessary to create an array of additional record-degree nodes, and when the program reads a good figure on record for each node penetration

Test data are as follows:

5
6
0 1
0 2
0 3
1 4
2 4
3 4

4. The specific code as follows:

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Scanner;
public class Main {
	static int n = 0;
	static int edgesNum = 0;
	static int inDegree[];
	static int count = 0;
	static List<Integer> graph [];
	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("输入图中顶点的个数: ");
		n = sc.nextInt();
		inDegree = new int[n];
		System.out.println("输入图中边的条数: ");
		edgesNum = sc.nextInt();
		//创建一个List数组
		graph = new ArrayList[n];
		for(int i = 0; i < n; i++){
			//初始化每一个集合中元素
			graph[i] = new ArrayList<Integer>();
		}
		System.out.println("输入图中边的起始顶点, 结束顶点和顶点之间的权值: ");
		for(int i = 0; i < edgesNum; i++){
			int u = sc.nextInt();
			int v = sc.nextInt();
			graph[u].add(v);
			inDegree[v]++;
		}
		boolean res = topologicalSort();
		if(res){
			System.out.println("拓扑排序成功");
		}else{
			System.out.println("拓扑排序失败");
		}
		sc.close();
	}
	
	private static boolean topologicalSort() {
		Queue<Integer> queue = new LinkedList<Integer>();
		//检查入度为零的顶点
		for(int i = 0; i < n; i++){
			if(inDegree[i] == 0){
				queue.add(i);
				//System.out.println(i);
			}
		}
		while(!queue.isEmpty()){
			int u = queue.poll();
			//输出拓扑排序序列
			System.out.print(u + " ");
			for(int i = 0; i < graph[u].size(); i++){
				int v = graph[u].get(i);
				inDegree[v]--;
				if(inDegree[v] == 0)queue.add(v); 
			}
			//记录访问过的节点的数目
			count++;
		}
		if(count == n) return true;
		return false;
	}
}

 

Guess you like

Origin blog.csdn.net/qq_39445165/article/details/91959284