(搬运)《算法导论》习题解答 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酱。这里是偶尔做做搬运工的水果君(^_^) )

相关文章:

  • 2021-09-28
  • 2022-02-20
  • 2022-12-23
  • 2021-05-17
  • 2021-05-23
  • 2021-07-30
  • 2021-05-30
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案