(搬运)《算法导论》习题解答 Chapter 22.1-1(入度和出度)

(搬运)《算法导论》习题解答 Chapter 22.1-1(入度和出度)

思路:遍历邻接列表即可;


伪代码:

for u 属于 Vertex
    for v属于 Adj[u]
        outdegree[u]++;
        indegree[v]++;


源代码:

package C22;

import java.util.Iterator;

public class C1_1 {
	static int[] indegree;
	static int[] outdegree;
	static Adjacent_List g;
	public static void main(String[] args) throws Exception {
		Adjacent_List adjacent_list = GraphFactory.getAdjacentListInstance("input\\22.1-1.txt");
		C1_1.g = adjacent_list;
		calculate_indegree_outdegree();
		System.out.println("入度");
		print(indegree);
		System.out.println("出度");
		print(outdegree);
	}
	public static void print(int[] arr){
		for(int i=0;i iter = g.getListByVertexIndex(i).iterator();
			while(iter.hasNext()){
				String vstr = iter.next();
				int v = g.getVertexIndex(vstr);
				indegree[v] ++;
				outdegree[i]++;
			}
		}
	}
}



(原文点此,索引目录。感谢xiazdong君 && Google酱。这里是偶尔做做搬运工的水果君(^_^) )

你可能感兴趣的:(算法导论)