Filip_Markoski

[ADSx] - Components of Connection

Jan 15th, 2018
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.16 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.util.*;
  4. import java.util.stream.IntStream;
  5.  
  6. class SLLNode<E> {
  7.     protected E element;
  8.     protected SLLNode<E> succ;
  9.  
  10.     public SLLNode(E elem, SLLNode<E> succ) {
  11.         this.element = elem;
  12.         this.succ = succ;
  13.     }
  14.  
  15.     @Override
  16.     public String toString() {
  17.         return element.toString();
  18.     }
  19. }
  20.  
  21. // Implementacija na redica
  22.  
  23. interface Queue<E> {
  24.  
  25.     // Elementi na redicata se objekti od proizvolen tip.
  26.  
  27.     // Metodi za pristap:
  28.  
  29.     public boolean isEmpty();
  30.     // Vrakja true ako i samo ako redicata e prazena.
  31.  
  32.     public int size();
  33.     // Ja vrakja dolzinata na redicata.
  34.  
  35.     public E peek();
  36.     // Go vrakja elementot na vrvot t.e. pocetokot od redicata.
  37.  
  38.     // Metodi za transformacija:
  39.  
  40.     public void clear();
  41.     // Ja prazni redicata.
  42.  
  43.     public void enqueue(E x);
  44.     // Go dodava x na kraj od redicata.
  45.  
  46.     public E dequeue();
  47.     // Go otstranuva i vrakja pochetniot element na redicata.
  48.  
  49. }
  50.  
  51. class LinkedQueue<E> implements Queue<E> {
  52.  
  53.     // Redicata e pretstavena na sledniot nacin:
  54.     // length go sodrzi brojot na elementi.
  55.     // Elementite se zachuvuvaat vo jazli dod SLL
  56.     // front i rear se linkovi do prviot i posledniot jazel soodvetno.
  57.     SLLNode<E> front, rear;
  58.     int length;
  59.  
  60.     // Konstruktor ...
  61.  
  62.     public LinkedQueue() {
  63.         clear();
  64.     }
  65.  
  66.     public boolean isEmpty() {
  67.         // Vrakja true ako i samo ako redicata e prazena.
  68.         return (length == 0);
  69.     }
  70.  
  71.     public int size() {
  72.         // Ja vrakja dolzinata na redicata.
  73.         return length;
  74.     }
  75.  
  76.     public E peek() {
  77.         // Go vrakja elementot na vrvot t.e. pocetokot od redicata.
  78.         if (front == null)
  79.             throw new NoSuchElementException();
  80.         return front.element;
  81.     }
  82.  
  83.     public void clear() {
  84.         // Ja prazni redicata.
  85.         front = rear = null;
  86.         length = 0;
  87.     }
  88.  
  89.     public void enqueue(E x) {
  90.         // Go dodava x na kraj od redicata.
  91.         SLLNode<E> latest = new SLLNode<E>(x, null);
  92.         if (rear != null) {
  93.             rear.succ = latest;
  94.             rear = latest;
  95.         } else
  96.             front = rear = latest;
  97.         length++;
  98.     }
  99.  
  100.     public E dequeue() {
  101.         // Go otstranuva i vrakja pochetniot element na redicata.
  102.         if (front != null) {
  103.             E frontmost = front.element;
  104.             front = front.succ;
  105.             if (front == null) rear = null;
  106.             length--;
  107.             return frontmost;
  108.         } else
  109.             throw new NoSuchElementException();
  110.     }
  111.  
  112. }
  113.  
  114. class GraphNode<E> {
  115.     private int index;//index (reden broj) na temeto vo grafot
  116.     private E ime;
  117.     private E prezime;
  118.     private LinkedList<GraphNode<E>> neighbors;
  119.  
  120.     public GraphNode(int index, E ime, E prezime) {
  121.         this.index = index;
  122.         this.ime = ime;
  123.         this.prezime = prezime;
  124.         neighbors = new LinkedList<GraphNode<E>>();
  125.     }
  126.  
  127.     public GraphNode(int index) {
  128.         this.index = index;
  129.  
  130.         neighbors = new LinkedList<GraphNode<E>>();
  131.     }
  132.  
  133.     boolean containsNeighbor(GraphNode<E> o) {
  134.         return neighbors.contains(o);
  135.     }
  136.  
  137.     void addNeighbor(GraphNode<E> o) {
  138.         neighbors.add(o);
  139.     }
  140.  
  141.     void removeNeighbor(GraphNode<E> o) {
  142.         if (neighbors.contains(o))
  143.             neighbors.remove(o);
  144.     }
  145.  
  146.  
  147.     public int getIndex() {
  148.         return index;
  149.     }
  150.  
  151.     public void setIndex(int index) {
  152.         this.index = index;
  153.     }
  154.  
  155.  
  156.     public LinkedList<GraphNode<E>> getNeighbors() {
  157.         return neighbors;
  158.     }
  159.  
  160.     public void setNeighbors(LinkedList<GraphNode<E>> neighbors) {
  161.         this.neighbors = neighbors;
  162.     }
  163. }
  164.  
  165. class Graph<E> {
  166.  
  167.     int num_nodes;
  168.     GraphNode<E> adjList[];
  169.  
  170.  
  171.     @SuppressWarnings("unchecked")
  172.     public Graph(int num_nodes) {
  173.         this.num_nodes = num_nodes;
  174.         adjList = (GraphNode<E>[]) new GraphNode[num_nodes];
  175.         for (int i = 0; i < num_nodes; i++)
  176.             adjList[i] = new GraphNode<E>(i);
  177.     }
  178.  
  179.     int adjacent(int x, int y) {
  180.         // proveruva dali ima vrska od jazelot so
  181.         // indeks x do jazelot so indeks y
  182.         return (adjList[x].containsNeighbor(adjList[y])) ? 1 : 0;
  183.     }
  184.  
  185.     void addEdge(int x, int y) {
  186.         // dodava vrska od jazelot so indeks x do jazelot so indeks y
  187.         if (!adjList[x].containsNeighbor(adjList[y])) {
  188.             adjList[x].addNeighbor(adjList[y]);
  189.         }
  190.     }
  191.  
  192.     void deleteEdge(int x, int y) {
  193.         adjList[x].removeNeighbor(adjList[y]);
  194.     }
  195.  
  196.  
  197.     int[] najdikomponenti(int start) {
  198.         //vasiot kod tuka
  199.  
  200.         /**
  201.          * https://www.geeksforgeeks.org/breadth-first-traversal-for-a-graph/
  202.          * BFS of a graph
  203.          * */
  204.  
  205.         List<Integer> toBeReturned = new ArrayList<>();
  206.         Queue<Integer> queue = new LinkedQueue<>();
  207.         boolean[] visited = new boolean[num_nodes];
  208.         toBeReturned.add(start);
  209.         visited[start] = true;
  210.         queue.enqueue(start);
  211.  
  212.         while (!queue.isEmpty()) {
  213.             int dequeue = queue.dequeue();
  214.             for (GraphNode<E> i : adjList[dequeue].getNeighbors()) {
  215.                 if (!visited[i.getIndex()]) {
  216.                     visited[i.getIndex()] = true;
  217.                     toBeReturned.add(i.getIndex());
  218.                     queue.enqueue(i.getIndex());
  219.                 }
  220.             }
  221.         }
  222.         return toBeReturned.stream().sorted().mapToInt(Integer::intValue).toArray();
  223.     }
  224.  
  225.  
  226.     @Override
  227.     public String toString() {
  228.         StringBuilder ret = new StringBuilder();
  229.         for (int i = 0; i < this.num_nodes; i++)
  230.             ret.append(i).append(": ").append(adjList[i]).append("\n");
  231.         return ret.toString();
  232.     }
  233.  
  234. }
  235.  
  236.  
  237. public class KomponentiSvrzanost {
  238.  
  239.     public static void main(String[] args) throws Exception {
  240.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  241.         int N = Integer.parseInt(br.readLine());
  242.  
  243.  
  244.         Graph<String> g = new Graph<String>(N);
  245.  
  246.         int sosedIndex;
  247.         int i_num_nodes;
  248.         String pom;
  249.         for (int i = 0; i < N; i++) {
  250.             i_num_nodes = Integer.parseInt(br.readLine());
  251.             for (int j = 0; j < i_num_nodes; j++) {
  252.                 pom = br.readLine();
  253.                 sosedIndex = Integer.parseInt(pom);
  254.  
  255.                 g.addEdge(i, sosedIndex);
  256.             }
  257.         }
  258.  
  259.  
  260.         int teme = Integer.parseInt(br.readLine());
  261.         int[] komponenti = g.najdikomponenti(teme);
  262.  
  263.         //vasiot kod tuka
  264.         //System.out.println(Arrays.toString(komponenti));
  265.         IntStream.range(0, komponenti.length).forEach(i -> System.out.println(komponenti[i]));
  266.  
  267.         br.close();
  268.     }
  269.  
  270. }
  271. /**
  272.  * For a given vertex, print the vertices of the same component of a given graph.
  273.  * Vertices belong to the same component if there is a path between them.
  274.  * First you are given N, the number of vertices,
  275.  * then for each vertex, the number of vertices it is connected with, and the ids of the vertices.
  276.  * Lastly, the id of the required vertex is given.
  277.  */
Advertisement
Add Comment
Please, Sign In to add comment