Advertisement
fueanta

Read File and Find Adjacent List & Matrix

Sep 26th, 2017
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.88 KB | None | 0 0
  1. /* Connections between nodes are given in the file
  2. 5
  3. 10
  4. 0,1
  5. 0,2
  6. 1,0
  7. 1,2
  8. 1,4
  9. 2,0
  10. 2,1
  11. 2,3
  12. 3,2
  13. 4,1
  14. read it and find adjacent list and adjacent matrix */
  15.  
  16. import java.util.*;
  17. import java.io.*;
  18.  
  19. public class AdjacentListMatrix {
  20.    
  21.     private static HashMap<String, ArrayList<String>> adjacentList;
  22.     private static int numOfNodes;
  23.     private static int numOfEdges;
  24.  
  25.     public static void main(String[] args) {
  26.         adjacentList = new HashMap<>();
  27.         readFile("Adjacent List.txt");
  28.         System.out.println("\n* Adjacent List:");
  29.         printAdjacentList();
  30.         System.out.println("\n* Adjacent Matrix:");
  31.         printAdjacentMatrix();
  32.     }
  33.  
  34.     public static void readFile(String fileName) {
  35.         try {
  36.             File file = new File(fileName);
  37.             Scanner scannedFile = new Scanner(file);
  38.  
  39.             numOfNodes = Integer.parseInt(scannedFile.nextLine());
  40.             numOfEdges = Integer.parseInt(scannedFile.nextLine());
  41.  
  42.             String tempKey = "";
  43.             ArrayList<String> tempList = new ArrayList<>();
  44.            
  45.             while(scannedFile.hasNextLine()) {
  46.                 String newLine = scannedFile.nextLine();
  47.                 String[] splittedLine = newLine.split(",");
  48.                
  49.                 if (tempKey.equals("")) {
  50.                     tempKey = splittedLine[0];
  51.                     tempList.add(splittedLine[1]);
  52.                 }
  53.                 else if (tempKey.equals(splittedLine[0])) {
  54.                     tempList.add(splittedLine[1]);
  55.                 }
  56.                 else if (!tempKey.equals(splittedLine[0])) {
  57.                     adjacentList.put(tempKey, tempList);
  58.                     tempKey = splittedLine[0];
  59.                     tempList = new ArrayList<>();
  60.                     tempList.add(splittedLine[1]);
  61.                 }
  62.  
  63.                 if (!scannedFile.hasNextLine()) {
  64.                     adjacentList.put(tempKey, tempList);
  65.                 }
  66.             }
  67.            
  68.         }
  69.         catch(FileNotFoundException ex) {
  70.             ex.printStackTrace();
  71.         }
  72.     }
  73.  
  74.     public static void printAdjacentList() {
  75.         for (String key : adjacentList.keySet()) {
  76.             System.out.print(key + " : ");
  77.             ArrayList<String> list = adjacentList.get(key);
  78.             for (String val : list) {
  79.                 System.out.print(val + " ");
  80.             }
  81.             System.out.println();
  82.         }
  83.     }
  84.  
  85.     public static void printAdjacentMatrix() {
  86.         for (int i = 0; i < numOfNodes; i++) {
  87.             ArrayList<String> list = adjacentList.get(Integer.toString(i));
  88.             for (int j = 0; j < numOfNodes; j++) {
  89.                 if (list.contains(Integer.toString(j)))
  90.                     System.out.print("1 ");
  91.                 else System.out.print("0 ");
  92.             }
  93.             System.out.println();
  94.         }
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement