Advertisement
fensa08

[APS] Kolokvium 2 - Komponenti na Svrzanost

Jan 21st, 2020
599
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.60 KB | None | 0 0
  1. Компоненти на сврзаност Problem 10 (0 / 0)
  2.  
  3. За дадено теме да се прикажат сите темиња кои припаѓаат на иста компонента на сврзаност во даден граф. Темињата припаѓаат на иста компонента на сврзаност во графот доколку постои пат помеѓу нив. На влез прво се внесува бројот на темиња, потоа за секое теме се пишува бројот на темиња со кои е поврзано и индексите на темињата. На крај се внесува индексот на темето за кое ќе се бара решението.
  4.  
  5. Име на класата (Java): KomponentiSvrzanost.
  6.  
  7.  
  8. =======================================================================================================================================
  9. import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
  10.  
  11. import java.io.BufferedReader;
  12. import java.io.InputStreamReader;
  13. import java.util.NoSuchElementException;
  14. import java.util.LinkedList;
  15. import java.util.Stack;
  16.  
  17. class SLLNode<E> {
  18.     protected E element;
  19.     protected SLLNode<E> succ;
  20.  
  21.     public SLLNode(E elem, SLLNode<E> succ) {
  22.         this.element = elem;
  23.         this.succ = succ;
  24.     }
  25.  
  26.     @Override
  27.     public String toString() {
  28.         return element.toString();
  29.     }
  30. }
  31.  
  32. // Implementacija na redica
  33.  
  34. interface Queue<E> {
  35.  
  36.     // Elementi na redicata se objekti od proizvolen tip.
  37.  
  38.     // Metodi za pristap:
  39.  
  40.     public boolean isEmpty ();
  41.     // Vrakja true ako i samo ako redicata e prazena.
  42.  
  43.     public int size ();
  44.     // Ja vrakja dolzinata na redicata.
  45.  
  46.     public E peek ();
  47.     // Go vrakja elementot na vrvot t.e. pocetokot od redicata.
  48.  
  49.     // Metodi za transformacija:
  50.  
  51.     public void clear ();
  52.     // Ja prazni redicata.
  53.  
  54.     public void enqueue (E x);
  55.     // Go dodava x na kraj od redicata.
  56.  
  57.     public E dequeue ();
  58.     // Go otstranuva i vrakja pochetniot element na redicata.
  59.  
  60. }
  61.  
  62. class LinkedQueue<E> implements Queue<E> {
  63.  
  64.     // Redicata e pretstavena na sledniot nacin:
  65.     // length go sodrzi brojot na elementi.
  66.     // Elementite se zachuvuvaat vo jazli dod SLL
  67.     // front i rear se linkovi do prviot i posledniot jazel soodvetno.
  68.     SLLNode<E> front, rear;
  69.     int length;
  70.  
  71.     // Konstruktor ...
  72.  
  73.     public LinkedQueue () {
  74.         clear();
  75.     }
  76.  
  77.     public boolean isEmpty () {
  78.         // Vrakja true ako i samo ako redicata e prazena.
  79.         return (length == 0);
  80.     }
  81.  
  82.     public int size () {
  83.         // Ja vrakja dolzinata na redicata.
  84.         return length;
  85.     }
  86.  
  87.     public E peek () {
  88.         // Go vrakja elementot na vrvot t.e. pocetokot od redicata.
  89.         if (front == null)
  90.             throw new NoSuchElementException();
  91.         return front.element;
  92.     }
  93.  
  94.     public void clear () {
  95.         // Ja prazni redicata.
  96.         front = rear = null;
  97.         length = 0;
  98.     }
  99.  
  100.     public void enqueue (E x) {
  101.         // Go dodava x na kraj od redicata.
  102.         SLLNode<E> latest = new SLLNode<E>(x, null);
  103.         if (rear != null) {
  104.             rear.succ = latest;
  105.             rear = latest;
  106.         } else
  107.             front = rear = latest;
  108.         length++;
  109.     }
  110.  
  111.     public E dequeue () {
  112.         // Go otstranuva i vrakja pochetniot element na redicata.
  113.         if (front != null) {
  114.             E frontmost = front.element;
  115.             front = front.succ;
  116.             if (front == null)  rear = null;
  117.             length--;
  118.             return frontmost;
  119.         } else
  120.             throw new NoSuchElementException();
  121.     }
  122.  
  123. }
  124.  
  125. class GraphNode<E> {
  126.     private int index;//index (reden broj) na temeto vo grafot
  127.     private E ime;
  128.     private E prezime;
  129.     private LinkedList<GraphNode<E>> neighbors;
  130.  
  131.     public GraphNode(int index, E ime, E prezime) {
  132.         this.index = index;
  133.         this.ime = ime;
  134.         this.prezime = prezime;
  135.         neighbors = new LinkedList<GraphNode<E>>();
  136.     }
  137.  
  138.     public GraphNode(int index) {
  139.         this.index = index;
  140.  
  141.         neighbors = new LinkedList<GraphNode<E>>();
  142.     }
  143.  
  144.     boolean containsNeighbor(GraphNode<E> o){
  145.         return neighbors.contains(o);
  146.     }
  147.  
  148.     void addNeighbor(GraphNode<E> o){
  149.         neighbors.add(o);
  150.     }
  151.  
  152.     void removeNeighbor(GraphNode<E> o){
  153.         if(neighbors.contains(o))
  154.             neighbors.remove(o);
  155.     }
  156.  
  157.  
  158.  
  159.     public int getIndex() {
  160.         return index;
  161.     }
  162.  
  163.     public void setIndex(int index) {
  164.         this.index = index;
  165.     }
  166.  
  167.  
  168.  
  169.  
  170.     public LinkedList<GraphNode<E>> getNeighbors() {
  171.         return neighbors;
  172.     }
  173.  
  174.     public void setNeighbors(LinkedList<GraphNode<E>> neighbors) {
  175.         this.neighbors = neighbors;
  176.     }
  177. }
  178.  
  179. class Graph<E> {
  180.  
  181.     int num_nodes;
  182.     GraphNode<E> adjList[];
  183.  
  184.  
  185.  
  186.     @SuppressWarnings("unchecked")
  187.     public Graph(int num_nodes) {
  188.         this.num_nodes = num_nodes;
  189.         adjList = (GraphNode<E>[]) new GraphNode[num_nodes];
  190.         for (int i = 0; i < num_nodes; i++)
  191.             adjList[i] = new GraphNode<E>(i);
  192.     }
  193.  
  194.     int adjacent(int x, int y) {
  195.         // proveruva dali ima vrska od jazelot so
  196.         // indeks x do jazelot so indeks y
  197.         return (adjList[x].containsNeighbor(adjList[y])) ? 1 : 0;
  198.     }
  199.  
  200.     void addEdge(int x, int y) {
  201.         // dodava vrska od jazelot so indeks x do jazelot so indeks y
  202.         if (!adjList[x].containsNeighbor(adjList[y])) {
  203.             adjList[x].addNeighbor(adjList[y]);
  204.         }
  205.     }
  206.  
  207.     void deleteEdge(int x, int y) {
  208.         adjList[x].removeNeighbor(adjList[y]);
  209.     }
  210.  
  211.     public void dfsVisit(boolean[] visited, int node, Stack<Integer> s){
  212.         if(!visited[node]){
  213.  
  214.             visited[node] = true;
  215.             s.push(node);
  216.             LinkedList<GraphNode<E>> neighbors = adjList[node].getNeighbors();
  217.             for(GraphNode<E> n : neighbors){
  218.                 if(!visited[n.getIndex()] && adjacent(n.getIndex(), node) == 1){
  219.                     dfsVisit(visited,n.getIndex(), s);
  220.                 }
  221.             }
  222.         }
  223.  
  224.     }
  225.     int [] najdikomponenti(int start){
  226.  
  227.         //vasiot kod tuka
  228.  
  229.         boolean visited[] = new boolean[this.num_nodes];
  230.         for(int i = 0; i < visited.length; i++){
  231.             visited[i] = false;
  232.         }
  233.  
  234.         Stack<Integer> s = new Stack<Integer>();
  235.         visited[start] = true;
  236.         LinkedList<GraphNode<E>> neigh = this.adjList[start].getNeighbors();
  237.         for(GraphNode<E> n : neigh){
  238.             if(adjacent(n.getIndex(),start) == 1){
  239.                 if(!visited[n.getIndex()]){
  240.                     dfsVisit(visited,n.getIndex(),s);
  241.                 }
  242.             }
  243.         }
  244.  
  245.  
  246.         int n = s.size();
  247.         int arr[] = new int[n+1];
  248.         Stack<Integer> r = new Stack<Integer>();
  249.        
  250.         s.push(start);
  251.         for(int i = 0; i <= n; i++){
  252.            arr[i] = s.pop();
  253.         }
  254.  
  255.         return arr;
  256.  
  257.  
  258.  
  259.     }
  260.  
  261.  
  262.     @Override
  263.     public String toString() {
  264.         String ret = new String();
  265.         for (int i = 0; i < this.num_nodes; i++)
  266.             ret += i + ": " + adjList[i] + "\n";
  267.         return ret;
  268.     }
  269.  
  270. }
  271.  
  272.  
  273.  
  274.  class KomponentiSvrzanost {
  275.  
  276.     public static void bubbleSort(int[] arr){
  277.  
  278.         for(int i = 0; i < arr.length - 1; i++){
  279.             for(int j = 0; j < arr.length - i - 1; j++){
  280.                 if(arr[j] > arr[j+ 1]){
  281.                     int temp = arr[j];
  282.                     arr[j] = arr[j+1];
  283.                     arr[j+1] =temp;
  284.                 }
  285.             }
  286.         }
  287.  
  288.     }
  289.  
  290.     public static void main(String[] args) throws Exception {
  291.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  292.         int N = Integer.parseInt(br.readLine());
  293.  
  294.  
  295.         Graph<String> g = new Graph<String>(N);
  296.  
  297.         int sosedIndex;
  298.         int i_num_nodes;
  299.         String pom;
  300.         for(int i=0;i<N;i++)
  301.         {
  302.             i_num_nodes = Integer.parseInt(br.readLine());
  303.             for(int j=0;j<i_num_nodes;j++)
  304.             {
  305.                 pom = br.readLine();
  306.                 sosedIndex = Integer.parseInt(pom);
  307.  
  308.                 g.addEdge(i,sosedIndex);
  309.             }
  310.         }
  311.  
  312.  
  313.         int teme = Integer.parseInt(br.readLine());
  314.         int[] komponenti =g.najdikomponenti(teme);
  315.         //vasiot kod tuka
  316.  
  317.  
  318.         bubbleSort(komponenti);
  319.         for(int i = 0; i < komponenti.length; i++){
  320.             System.out.println(komponenti[i]);
  321.         }
  322.        
  323.  
  324.         br.close();
  325.     }
  326.  
  327. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement