程式扎記: [ Data Structures with Java ] Section 25.2 : Strongly Connected Components - Example 25.2

標籤

2011年5月5日 星期四

[ Data Structures with Java ] Section 25.2 : Strongly Connected Components - Example 25.2


Preface :
The example outputs the strong components for the graph in below figure. Assume that g is the graph and componentList is the ArrayList of strong component lists returned by the method strongComponents().

Corresponding graph file :
- scc.dat :
7
A B C D E F G
8
A B 1
B C 1
C A 1
C E 1
E D 1
D G 1
G F 1
F D 1


- Example 25.2 :
  1. public static void example25_2(){  
  2.     JGraph g = JGraph.readGraph("scc.dat");  
  3.     ArrayList> componentList = new ArrayList>();  
  4.     JGraph.strongComponents(g, componentList);  
  5.     int scCount = 0;  
  6.     for(LinkedList component: componentList) {  
  7.         scCount++;  
  8.         System.out.println("Component "+scCount+": "+component);  
  9.     }  
  10. }  


Output :
Component 1: [A, C, B]
Component 2: [E]
Component 3: [D, F, G]

沒有留言:

張貼留言

網誌存檔

關於我自己

我的相片
Where there is a will, there is a way!