Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.63 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3.  
  4. @SuppressWarnings("serial")
  5. class CollectionException extends Exception {
  6.     public CollectionException(String msg) {
  7.          super(msg);
  8.     }
  9. }
  10.  
  11.  
  12. interface Collection {
  13.     static final String ERR_MSG_EMPTY = "Collection is empty.";
  14.     static final String ERR_MSG_FULL = "Collection is full.";
  15.     boolean isEmpty();
  16.     boolean isFull();
  17.     int count();
  18.     String toStg();
  19. }
  20.  
  21. class Edge {
  22.  
  23.     int vert1; int vert2;
  24.  
  25.     public Edge(int v1, int v2) {
  26.         this.vert1 = v1; this.vert2 = v2;
  27.     }
  28. }
  29. //copy of snipsets of Naloga1
  30. class Deque<T> implements Collection {
  31.     private static final int DEFAULT_CAPACITY = 256;
  32.  
  33.     @SuppressWarnings("unchecked")
  34.     T[] items = (T[]) new Object[DEFAULT_CAPACITY];
  35.     int front;
  36.     int back;
  37.     int count;
  38.  
  39.     @SuppressWarnings("unchecked")
  40.     public Deque() {
  41.         @SuppressWarnings("unused")
  42.         T[] items = (T[]) new Object[DEFAULT_CAPACITY];
  43.         @SuppressWarnings("unused")
  44.         int front = 0;
  45.         @SuppressWarnings("unused")
  46.         int back = 0;
  47.         @SuppressWarnings("unused")
  48.         int count = 0;
  49.     }
  50.  
  51.     public int count() {
  52.         return count;
  53.     }
  54.  
  55.     public boolean isEmpty() {
  56.         return count == 0;
  57.     }
  58.  
  59.     public boolean isFull() {
  60.         return count == items.length;
  61.     }
  62.    
  63.     public String toStg() {
  64.         StringBuilder sb = new StringBuilder("[");
  65.         if (count > 0) {
  66.                 sb.append(items[front]);
  67.                 for (int i = 1; i < count; i++) {
  68.                         sb.append(", ");
  69.                         sb.append(items[(front + i) % items.length]);
  70.                 }
  71.         }
  72.         sb.append("]");
  73.         return sb.toString();
  74.     }
  75.  
  76.     public T front() throws CollectionException {
  77.         if (isEmpty())
  78.                 throw new CollectionException(ERR_MSG_EMPTY);
  79.         return items[front];
  80.     }
  81.  
  82.     public T back() throws CollectionException {
  83.         if (isEmpty())
  84.                 throw new CollectionException(ERR_MSG_EMPTY);
  85.         return items[(back - 1 + items.length) % items.length];
  86.     }
  87.  
  88.     public void enqueue(T x) throws CollectionException {
  89.         if (isFull())
  90.                 throw new CollectionException(ERR_MSG_FULL);
  91.         items[back] = x;
  92.         back = (back + 1) % items.length;
  93.         count++;
  94.     }
  95.  
  96.     public void enqueueFront(T x) throws CollectionException {
  97.         if (isFull())
  98.                 throw new CollectionException(ERR_MSG_FULL);
  99.         front = (front - 1 + items.length) % items.length;
  100.         items[front] = x;
  101.         count++;
  102.     }
  103.  
  104.     public T dequeue() throws CollectionException {
  105.         if (isEmpty())
  106.                 throw new CollectionException(ERR_MSG_EMPTY);
  107.         T temp = items[front];
  108.         items[front] = null;
  109.         front = (front + 1) % items.length;
  110.         count--;
  111.         return temp;
  112.     }
  113.  
  114.     public T dequeueBack() throws CollectionException {
  115.         if (isEmpty())
  116.                 throw new CollectionException(ERR_MSG_EMPTY);
  117.         T temp = items[(back - 1 + items.length) % items.length];
  118.         items[(back - 1 + items.length) % items.length] = null;
  119.         back = (back - 1 + items.length) % items.length;
  120.         count--;
  121.         return temp;
  122.     }
  123. }
  124.  
  125. /**
  126.  *
  127.  */
  128. class Graph {
  129.  
  130.     public String type;
  131.     public int numVerts;
  132.     public Edge[] edges;
  133.  
  134.     private String entry;
  135.     private String exit;
  136.     private String order;
  137.     private int[] paths;
  138.  
  139.     public Graph(String type, int numVerts, Edge[] edges) {
  140.         this.type = type;
  141.         this.numVerts = numVerts;
  142.         this.edges = edges;
  143.     }
  144.  
  145.     public void walks(int length) {
  146.         int[][] matrix = new int[numVerts][numVerts];
  147.         for (int i = 0; i < edges.length; i++) {
  148.             matrix[edges[i].vert1][edges[i].vert2] = 1;
  149.             if (type.equals("undirected"))
  150.                 matrix[edges[i].vert2][edges[i].vert1] = 1;
  151.         }
  152.         int[][] walks = matrix;
  153.         for (int i = 1; i < length; i++) {
  154.             walks = multiplyMatrices(walks, matrix);
  155.         }
  156.         for (int i = 0; i < numVerts; i++) {
  157.             for (int j = 0; j < numVerts; j++) {
  158.                 System.out.print(walks[i][j] + " ");   
  159.             }
  160.             System.out.println();
  161.         }
  162.     }
  163.  
  164.    
  165.  
  166.     public void dfsFull() {
  167.         boolean[] visited = new boolean[numVerts];
  168.         entry = "";
  169.         exit = "";
  170.         for (int i = 0; i < numVerts; i++) {
  171.             if (!visited[i])
  172.                 dfs(i, visited);
  173.         }
  174.         System.out.println(entry);
  175.         System.out.println(exit);
  176.     }
  177.  
  178.     private void dfs(int v, boolean[] visited) {
  179.         entry += Integer.toString(v) + " ";
  180.         visited[v] = true;
  181.         int[] neighbors = getNeighbors(v);
  182.         for (int i = 0; i < neighbors.length; i++) {
  183.             if (!visited[neighbors[i]])
  184.                 dfs(neighbors[i], visited);
  185.         }
  186.         exit += Integer.toString(v) + " ";
  187.     }
  188.  
  189.     public void bfsFull() throws Exception {
  190.         boolean[] visited = new boolean[numVerts];
  191.         order = "";
  192.         for (int i = 0; i < numVerts; i++) {
  193.             if (!visited[i])
  194.                 bfs(i, visited, "bfs");
  195.         }
  196.         System.out.println(order);
  197.     }
  198.  
  199.     private void bfs(int v, boolean[] visited, String mode) throws Exception {
  200.         Deque<Integer> q = new Deque<Integer>();
  201.         if (mode.equals("sp"))
  202.             paths[v] = 0;
  203.         visited[v] = true;
  204.         q.enqueue(v);
  205.         while (!q.isEmpty()) {
  206.             v = q.dequeue();
  207.             if (mode.equals("bfs"))
  208.                 order += v + " ";
  209.             int[] neighbors = getNeighbors(v);
  210.             for (int i = 0; i < neighbors.length; i++) {
  211.                 if (!visited[neighbors[i]]) {
  212.                     if (mode.equals("sp"))
  213.                         paths[neighbors[i]] = paths[v] + 1;
  214.                     visited[neighbors[i]] = true;
  215.                     q.enqueue(neighbors[i]);
  216.                 }
  217.             }
  218.         }
  219.     }
  220.  
  221.     public void sp(int v) throws Exception {
  222.         boolean[] visited = new boolean[numVerts];
  223.         order = "";
  224.         paths = new int[numVerts];
  225.         for (int i = 0; i < numVerts; i++) {
  226.             paths[i] = -1;
  227.         }
  228.         bfs(v, visited, "sp");
  229.         for (int i = 0; i < numVerts; i++) {
  230.             System.out.println(i + " " + paths[i]);
  231.         }
  232.     }
  233.  
  234.     private int[][] multiplyMatrices(int[][] m1, int[][] m2) {
  235.         int[][] multipliedMatrix = new int[m1.length][m1.length];
  236.         int sum = 0;
  237.         for (int i = 0; i < m1.length; i++) {
  238.             for (int j = 0; j < m2[0].length; j++) {
  239.                 for (int k = 0; k < m1[0].length; k++) {
  240.                     sum += m1[i][k] * m2[k][j];
  241.                 }
  242.                 multipliedMatrix[i][j] = sum;
  243.                 sum = 0;
  244.             }
  245.         };
  246.         return multipliedMatrix;
  247.     }
  248.  
  249.     private int countOccurences(int element) {
  250.         int counter = 0;
  251.         for (int i = 0; i < edges.length; i++) {
  252.             if (type.equals("directed")) {
  253.                 if (edges[i].vert1 == element)
  254.                     counter++;
  255.             } else {
  256.                 if (edges[i].vert1 == element || edges[i].vert2 == element) {
  257.                     counter++;
  258.                 }
  259.             }
  260.         }
  261.         return counter;
  262.     }
  263.  
  264.     private int[] getNeighbors(int v) {
  265.         int[] neighbors = new int[countOccurences(v)];
  266.         int index = 0;
  267.         for (int i = 0; i < edges.length; i++) {
  268.             if (edges[i].vert1 == v) {
  269.                 neighbors[index] = edges[i].vert2;
  270.                 index++;
  271.             }
  272.             if (type.equals("undirected")) {
  273.                 if (edges[i].vert2 == v) {
  274.                     neighbors[index] = edges[i].vert1;
  275.                     index++;
  276.                 }
  277.             }
  278.         }
  279.         insertionSort(neighbors);
  280.         return neighbors;
  281.     }
  282.  
  283.     private void insertionSort(int arr[])
  284.     {
  285.         int n = arr.length;
  286.         for (int i=1; i<n; ++i)
  287.         {
  288.             int key = arr[i];
  289.             int j = i-1;
  290.  
  291.             /* Move elements of arr[0..i-1], that are
  292.                greater than key, to one position ahead
  293.                of their current position */
  294.             while (j>=0 && arr[j] > key)
  295.             {
  296.                 arr[j+1] = arr[j];
  297.                 j = j-1;
  298.             }
  299.             arr[j+1] = key;
  300.         }
  301.     }
  302.  
  303. }
  304. /**
  305.  * @author Jure Zajc
  306.  *
  307.  */
  308. public class Naloga3 {
  309.  
  310.     /**
  311.      * @param args
  312.      * @throws Exception
  313.      */
  314.     public static void main(String[] args) throws Exception {
  315.         // TODO Auto-generated method stub
  316.        
  317.         @SuppressWarnings("resource")
  318.         Scanner scanner = new Scanner(System.in);
  319.         String gType = scanner.next();
  320.         int nVerts = Integer.parseInt(scanner.nextLine().trim());
  321.         StringBuilder sb = new StringBuilder();
  322.         StringBuilder sb1 = new StringBuilder();
  323.         while (scanner.hasNextLine()) {
  324.             String[] line = scanner.nextLine().trim().split(" ");
  325.             sb.append(line[0] + ":");
  326.             sb1.append(line[1] + ":");         
  327.         }
  328.         String[] vert1 = sb.toString().split(":");
  329.         String[] vert2 = sb1.toString().split(":");
  330.         Edge[] edges = new Edge[vert1.length];
  331.         for (int i = 0; i < vert1.length; i++) {
  332.             edges[i] = new Edge(Integer.parseInt(vert1[i]), Integer.parseInt(vert2[i]));
  333.         }
  334.         Graph graph = new Graph(gType, nVerts, edges);
  335.         switch(scanner.next()){
  336.         case "walks":   graph.walks(Integer.parseInt(scanner.next())); break;
  337.         case "dfs":     graph.dfsFull(); break;
  338.         case "bfs":     graph.bfsFull();break;
  339.         case "sp":      graph.sp(Integer.parseInt(scanner.next())); break;
  340.         }
  341.     }
  342. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement