Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.util.Arrays;
- class Graph<E> {
- int num_nodes; // broj na jazli
- E nodes[]; // informacija vo jazlite - moze i ne mora?
- int adjMat[][]; // matrica na sosednost
- @SuppressWarnings("unchecked")
- public Graph(int num_nodes) {
- this.num_nodes = num_nodes;
- nodes = (E[]) new Object[num_nodes];
- adjMat = new int[num_nodes][num_nodes];
- for (int i = 0; i < this.num_nodes; i++)
- for (int j = 0; j < this.num_nodes; j++)
- adjMat[i][j] = 0;
- }
- int adjacent(int x, int y) { // proveruva dali ima vrska od jazelot so
- // indeks x do jazelot so indeks y
- return (adjMat[x][y] != 0) ? 1 : 0;
- }
- void addEdge(int x, int y) { // dodava vrska megu jazlite so indeksi x i y
- adjMat[x][y] = 1;
- adjMat[y][x] = 1;
- }
- void deleteEdge(int x, int y) {
- // ja brise vrskata megu jazlite so indeksi x i y
- adjMat[x][y] = 0;
- adjMat[y][x] = 0;
- }
- E get_node_value(int x) { // ja vraka informacijata vo jazelot so indeks x
- return nodes[x];
- }
- void set_node_value(int x, E a) { // ja postavuva informacijata vo jazelot
- // so indeks na a
- nodes[x] = a;
- }
- @Override
- public String toString() {
- StringBuilder ret = new StringBuilder(" ");
- for (int i = 0; i < num_nodes; i++)
- ret.append(nodes[i]).append(" ");
- ret.append("\n");
- for (int i = 0; i < num_nodes; i++) {
- ret.append(nodes[i]).append(" ");
- for (int j = 0; j < num_nodes; j++)
- ret.append(adjMat[i][j]).append(" ");
- ret.append("\n");
- }
- return ret.toString();
- }
- public void printMatrix() {
- for (int i = 0; i < num_nodes; i++) {
- for (int j = 0; j < num_nodes; j++)
- System.out.print(adjMat[i][j] + " ");
- System.out.println();
- }
- }
- public int pathCount(int nodeIndex, int pathLength) {
- int[][] matrixToPowerPathLength = powerMatrix(pathLength);
- return Arrays.stream(matrixToPowerPathLength[nodeIndex]).sum();
- }
- /**
- * Based on discrete mathematics
- * get the adjMatrix to adjMatrix^(length of path)
- * then just look at the given node's entry
- * https://math.stackexchange.com/questions/1890620/finding-path-lengths-by-the-power-of-adjacency-matrix-of-an-undirected-graph
- * */
- private int[][] powerMatrix(int pathLength) {
- int[][] result = adjMat;
- for (int i = 1; i < pathLength; i++) {
- result = multiplyMatrix(result, adjMat);
- }
- return result;
- }
- private int[][] multiplyMatrix(int[][] result, int[][] adjMat) {
- int[][] resultMultiply = new int[result.length][adjMat[0].length];
- for (int i = 0; i < result.length; i++)
- for (int j = 0; j < adjMat[0].length; j++)
- for (int k = 0; k < adjMat.length; k++)
- resultMultiply[i][j] += result[i][k] * adjMat[k][j];
- return resultMultiply;
- }
- }
- public class PathCount {
- public static void main(String[] args) throws Exception {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- int N = Integer.parseInt(br.readLine());
- Graph<Integer> g = new Graph<>(N);
- int M = Integer.parseInt(br.readLine());
- for (int i = 0; i < M; i++) {
- String line = br.readLine();
- String edgeLine[] = line.split(" ");
- g.addEdge(Integer.parseInt(edgeLine[0]), Integer.parseInt(edgeLine[1]));
- }
- int nodeIndex = Integer.parseInt(br.readLine());
- int pathLength = Integer.parseInt(br.readLine());
- System.out.println(g.pathCount(nodeIndex, pathLength));
- br.close();
- }
- }
- /**
- * Print the total number of paths with length N,
- * that start from a fixed vertex V, in an undirected, unweigthed graph.
- * <p>
- * 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.
- * <p>
- * Output: Print the total number of paths with length N. Vertices may repeat.
- * <p>
- * 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.
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement