(搬运)《算法导论》习题解答 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]++;
}
}
}
}