Advertisement
Filip_Markoski

[ADSx] - Total number of Roads

Jan 15th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.45 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.util.Arrays;
  4.  
  5. class Graph<E> {
  6.  
  7.     int num_nodes; // broj na jazli
  8.     E nodes[]; // informacija vo jazlite - moze i ne mora?
  9.     int adjMat[][]; // matrica na sosednost
  10.  
  11.     @SuppressWarnings("unchecked")
  12.     public Graph(int num_nodes) {
  13.         this.num_nodes = num_nodes;
  14.         nodes = (E[]) new Object[num_nodes];
  15.         adjMat = new int[num_nodes][num_nodes];
  16.  
  17.         for (int i = 0; i < this.num_nodes; i++)
  18.             for (int j = 0; j < this.num_nodes; j++)
  19.                 adjMat[i][j] = 0;
  20.     }
  21.  
  22.     int adjacent(int x, int y) { // proveruva dali ima vrska od jazelot so
  23.         // indeks x do jazelot so indeks y
  24.         return (adjMat[x][y] != 0) ? 1 : 0;
  25.     }
  26.  
  27.     void addEdge(int x, int y) { // dodava vrska megu jazlite so indeksi x i y
  28.         adjMat[x][y] = 1;
  29.         adjMat[y][x] = 1;
  30.     }
  31.  
  32.     void deleteEdge(int x, int y) {
  33.         // ja brise vrskata megu jazlite so indeksi x i y
  34.         adjMat[x][y] = 0;
  35.         adjMat[y][x] = 0;
  36.     }
  37.  
  38.     E get_node_value(int x) { // ja vraka informacijata vo jazelot so indeks x
  39.         return nodes[x];
  40.     }
  41.  
  42.     void set_node_value(int x, E a) { // ja postavuva informacijata vo jazelot
  43.         // so indeks na a
  44.         nodes[x] = a;
  45.     }
  46.  
  47.     @Override
  48.     public String toString() {
  49.         StringBuilder ret = new StringBuilder("  ");
  50.         for (int i = 0; i < num_nodes; i++)
  51.             ret.append(nodes[i]).append(" ");
  52.         ret.append("\n");
  53.         for (int i = 0; i < num_nodes; i++) {
  54.             ret.append(nodes[i]).append(" ");
  55.             for (int j = 0; j < num_nodes; j++)
  56.                 ret.append(adjMat[i][j]).append(" ");
  57.             ret.append("\n");
  58.         }
  59.         return ret.toString();
  60.     }
  61.  
  62.     public void printMatrix() {
  63.         for (int i = 0; i < num_nodes; i++) {
  64.             for (int j = 0; j < num_nodes; j++)
  65.                 System.out.print(adjMat[i][j] + " ");
  66.             System.out.println();
  67.         }
  68.     }
  69.  
  70.     public int pathCount(int nodeIndex, int pathLength) {
  71.         int[][] matrixToPowerPathLength = powerMatrix(pathLength);
  72.         return Arrays.stream(matrixToPowerPathLength[nodeIndex]).sum();
  73.     }
  74.  
  75.     /**
  76.      * Based on discrete mathematics
  77.      * get the adjMatrix to adjMatrix^(length of path)
  78.      * then just look at the given node's entry
  79.      * https://math.stackexchange.com/questions/1890620/finding-path-lengths-by-the-power-of-adjacency-matrix-of-an-undirected-graph
  80.      * */
  81.  
  82.     private int[][] powerMatrix(int pathLength) {
  83.         int[][] result = adjMat;
  84.         for (int i = 1; i < pathLength; i++) {
  85.             result = multiplyMatrix(result, adjMat);
  86.         }
  87.  
  88.         return result;
  89.     }
  90.  
  91.     private int[][] multiplyMatrix(int[][] result, int[][] adjMat) {
  92.         int[][] resultMultiply = new int[result.length][adjMat[0].length];
  93.         for (int i = 0; i < result.length; i++)
  94.             for (int j = 0; j < adjMat[0].length; j++)
  95.                 for (int k = 0; k < adjMat.length; k++)
  96.                     resultMultiply[i][j] += result[i][k] * adjMat[k][j];
  97.         return resultMultiply;
  98.     }
  99.  
  100. }
  101.  
  102. public class PathCount {
  103.  
  104.     public static void main(String[] args) throws Exception {
  105.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  106.         int N = Integer.parseInt(br.readLine());
  107.         Graph<Integer> g = new Graph<>(N);
  108.         int M = Integer.parseInt(br.readLine());
  109.         for (int i = 0; i < M; i++) {
  110.             String line = br.readLine();
  111.             String edgeLine[] = line.split(" ");
  112.             g.addEdge(Integer.parseInt(edgeLine[0]), Integer.parseInt(edgeLine[1]));
  113.         }
  114.         int nodeIndex = Integer.parseInt(br.readLine());
  115.         int pathLength = Integer.parseInt(br.readLine());
  116.         System.out.println(g.pathCount(nodeIndex, pathLength));
  117.         br.close();
  118.  
  119.     }
  120.  
  121. }
  122. /**
  123.  * Print the total number of paths with length N,
  124.  * that start from a fixed vertex V, in an undirected, unweigthed graph.
  125.  * <p>
  126.  * Input: The first and second line contain the number of vertices and edges, then follow the edges of the graph. After than, the starting vertex V is given, and in the last line is the number N.
  127.  * <p>
  128.  * Output: Print the total number of paths with length N. Vertices may repeat.
  129.  * <p>
  130.  * In the sample test case, there are two paths 0 1 2 and 0 1 0, Which start from 0, and have a length of 2.
  131.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement