Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. package acm;
  2.  
  3. import java.util.*;
  4. import java.io.*;
  5.  
  6. public class GraphTraversal {
  7.  
  8.     public static void main(String[] args) throws IOException {
  9.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  10.         String testCases = (br.readLine());
  11.         //while (testCases!="") {
  12.             int totalNodes;
  13.             int totalEdges;
  14.             String ne = br.readLine();
  15.             totalNodes = Integer.parseInt(ne.split(" ")[0]);
  16.             totalEdges = Integer.parseInt(ne.split(" ")[1]);
  17.             LinkedList<LinkedList> graphInfo = new LinkedList<LinkedList>();
  18.             for(int i=0;i<totalNodes;i++){
  19.                 LinkedList al=new LinkedList();
  20.                 graphInfo.add(i,al);
  21.             }
  22.             int edge = 1;
  23.             String aPlusB = br.readLine();
  24.             int root=Integer.parseInt(aPlusB.split(" ")[0]);
  25.             while (edge <= totalEdges) {
  26.                 int A = Integer.parseInt(aPlusB.split(" ")[0]);
  27.                 int B = Integer.parseInt(aPlusB.split(" ")[1]);
  28.                 processGraphInfo(graphInfo, A, B);
  29.                 aPlusB = br.readLine();
  30.                 edge++;
  31.             }
  32.            
  33.             for (int i = 0; i < graphInfo.size(); i++) {
  34.                 LinkedList al = graphInfo.get(i);
  35.                 Collections.sort(al);
  36.             }
  37.             System.out.println(testCases);
  38.             //Breadth First;
  39.             Queue q=new LinkedList();
  40.             q.add((Integer)root);
  41.             for(int i=0;i<graphInfo.size();i++){
  42.                 LinkedList ll=(LinkedList)graphInfo.get(i);
  43.                 for(int j=0;j<ll.size();j++){
  44.                     Integer num=(Integer)ll.get(j);
  45.                     if(q.contains(num)){
  46.                         continue;
  47.                     }else{
  48.                         q.add(num);
  49.                     }
  50.                 }
  51.             }
  52.             int i=1;
  53.             int queueSize=q.size();
  54.             while(!q.isEmpty()){
  55.                 if(i==queueSize){
  56.                     System.out.print(q.poll());
  57.                 }else{
  58.                     System.out.print(q.poll()+", ");
  59.                 }
  60.                 i++;
  61.             }
  62.             System.out.println("");
  63.             //Depth First
  64.             Stack stk=new Stack();
  65.             int j=0;
  66.             while(true){
  67.                 for(int i1=0;i1<graphInfo.size();i1++){
  68.                     LinkedList ll=(LinkedList)graphInfo.get(i1);
  69.                     for(int i2=j;i2<ll.size();i2++){
  70.                         Integer num=(Integer)ll.get(i2);
  71.                         if(root!=num && !stk.contains(num)){
  72.                             stk.add(num);
  73.                             break;
  74.                         }else{
  75.                             continue;
  76.                         }
  77.                     }
  78.                 }
  79.                 if(stk.size()!=(totalNodes-1)){
  80.                     j++;
  81.                 }else if(stk.size()==(totalNodes-1)){
  82.                     stk.add(root);
  83.                     break;
  84.                 }
  85.             }
  86.            
  87.             while(!q.isEmpty()){
  88.                 if(i==queueSize){
  89.                     System.out.print(q.poll());
  90.                 }else{
  91.                     System.out.print(q.poll()+", ");
  92.                 }
  93.                 i++;
  94.             }
  95.             while(!stk.isEmpty()){
  96.                 if(stk.size()==1){
  97.                     System.out.print(stk.pop());
  98.                 }else{
  99.                     System.out.print(stk.pop()+", ");
  100.                 }
  101.                
  102.             }
  103.         //  testCases = (br.readLine());
  104.         //}
  105.     }
  106.  
  107.     // Adjacency List
  108.     public static void processGraphInfo(LinkedList ll, int a, int b) {
  109.         LinkedList tmpA=(LinkedList)ll.get(a);
  110.         LinkedList tmpB=(LinkedList)ll.get(b);
  111.         tmpA.add(b);
  112.         tmpB.add(a);
  113.     }
  114. }