mvCode

GraphCreate AIPS, Креирање на граф

Dec 29th, 2019
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.18 KB | None | 0 0
  1.  
  2. import javax.imageio.IIOException;
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.NoSuchElementException;
  7. import java.util.Stack;
  8.  
  9. class LinkedQueue<E> implements Queue<E> {
  10.  
  11.     // Redicata e pretstavena na sledniot nacin:
  12.     // length go sodrzi brojot na elementi.
  13.     // Elementite se zachuvuvaat vo jazli dod SLL
  14.     // front i rear se linkovi do prviot i posledniot jazel soodvetno.
  15.     SLLNode<E> front, rear;
  16.     int length;
  17.  
  18.     // Konstruktor ...
  19.  
  20.     public LinkedQueue () {
  21.         clear();
  22.     }
  23.  
  24.     public boolean isEmpty () {
  25.         // Vrakja true ako i samo ako redicata e prazena.
  26.         return (length == 0);
  27.     }
  28.  
  29.     public int size () {
  30.         // Ja vrakja dolzinata na redicata.
  31.         return length;
  32.     }
  33.  
  34.     public E peek () {
  35.         // Go vrakja elementot na vrvot t.e. pocetokot od redicata.
  36.         if (front == null)
  37.             throw new NoSuchElementException();
  38.         return front.element;
  39.     }
  40.  
  41.     public void clear () {
  42.         // Ja prazni redicata.
  43.         front = rear = null;
  44.         length = 0;
  45.     }
  46.  
  47.     public void enqueue (E x) {
  48.         // Go dodava x na kraj od redicata.
  49.         SLLNode<E> latest = new SLLNode<E>(x, null);
  50.         if (rear != null) {
  51.             rear.succ = latest;
  52.             rear = latest;
  53.         } else
  54.             front = rear = latest;
  55.         length++;
  56.     }
  57.  
  58.     public E dequeue () {
  59.         // Go otstranuva i vrakja pochetniot element na redicata.
  60.         if (front != null) {
  61.             E frontmost = front.element;
  62.             front = front.succ;
  63.             if (front == null)  rear = null;
  64.             length--;
  65.             return frontmost;
  66.         } else
  67.             throw new NoSuchElementException();
  68.     }
  69.  
  70. }
  71.  
  72.  
  73. interface Queue<E> {
  74.  
  75.     // Elementi na redicata se objekti od proizvolen tip.
  76.  
  77.     // Metodi za pristap:
  78.  
  79.     public boolean isEmpty ();
  80.     // Vrakja true ako i samo ako redicata e prazena.
  81.  
  82.     public int size ();
  83.     // Ja vrakja dolzinata na redicata.
  84.  
  85.     public E peek ();
  86.     // Go vrakja elementot na vrvot t.e. pocetokot od redicata.
  87.  
  88.     // Metodi za transformacija:
  89.  
  90.     public void clear ();
  91.     // Ja prazni redicata.
  92.  
  93.     public void enqueue (E x);
  94.     // Go dodava x na kraj od redicata.
  95.  
  96.     public E dequeue ();
  97.     // Go otstranuva i vrakja pochetniot element na redicata.
  98.  
  99. }
  100.  
  101. class SLLNode<E> {
  102.     protected E element;
  103.     protected SLLNode<E> succ;
  104.  
  105.     public SLLNode(E elem, SLLNode<E> succ) {
  106.         this.element = elem;
  107.         this.succ = succ;
  108.     }
  109.  
  110.     @Override
  111.     public String toString() {
  112.         return element.toString();
  113.     }
  114. }
  115.  
  116. class Graph<E> {
  117.  
  118.     int num_nodes; // broj na jazli
  119.     E nodes[];    // informacija vo jazlite - moze i ne mora?
  120.     int adjMat[][];  // matrica na sosednost
  121.  
  122.     @SuppressWarnings("unchecked")
  123.     public Graph(int num_nodes) {
  124.         this.num_nodes = num_nodes;
  125.         nodes = (E[]) new Object[num_nodes];
  126.         adjMat = new int[num_nodes][num_nodes];
  127.  
  128.         for(int i=0;i<this.num_nodes;i++)
  129.             for(int j=0;j<this.num_nodes;j++)
  130.                 adjMat[i][j]=0;
  131.     }
  132.  
  133.  
  134.  
  135.     public Graph(int num_nodes, E[] nodes) {
  136.         this.num_nodes = num_nodes;
  137.         this.nodes = nodes;
  138.         adjMat = new int[num_nodes][num_nodes];
  139.  
  140.         for(int i=0;i<this.num_nodes;i++)
  141.             for(int j=0;j<this.num_nodes;j++)
  142.                 adjMat[i][j]=0;
  143.     }
  144.  
  145.  
  146.  
  147.     int adjacent(int x,int y)
  148.     {  // proveruva dali ima vrska od jazelot so indeks x do jazelot so indeks y
  149.         return (adjMat[x][y]!=0)?1:0;
  150.     }
  151.  
  152.     void addEdge(int x,int y)
  153.     {  // dodava vrska megu jazlite so indeksi x i y
  154.         adjMat[x][y]=1;
  155.         adjMat[y][x]=1;
  156.     }
  157.  
  158.     void deleteEdge(int x,int y)
  159.     {
  160.         // ja brise vrskata megu jazlite so indeksi x i y
  161.         adjMat[x][y]=0;
  162.         adjMat[y][x]=0;
  163.     }
  164.  
  165.     // Moze i ne mora?
  166.     E get_node_value(int x)
  167.     {  // ja vraka informacijata vo jazelot so indeks x
  168.         return nodes[x];
  169.     }
  170.  
  171.     // Moze i ne mora?
  172.     void set_node_value(int x, E a)
  173.     {  // ja postavuva informacijata vo jazelot so indeks na a
  174.         nodes[x]=a;
  175.     }
  176.  
  177.     public int getNum_nodes() {
  178.         return num_nodes;
  179.     }
  180.  
  181.     public void setNum_nodes(int num_nodes) {
  182.         this.num_nodes = num_nodes;
  183.     }
  184.  
  185.     void dfsSearch(int node) {
  186.         boolean visited[] = new boolean[num_nodes];
  187.         for (int i = 0; i < num_nodes; i++)
  188.             visited[i] = false;
  189.         dfsRecursive(node, visited);
  190.     }
  191.  
  192.     void dfsRecursive(int node, boolean visited[]) {
  193.         visited[node] = true;
  194.         System.out.println(node + ": " + nodes[node]);
  195.  
  196.         for (int i = 0; i < this.num_nodes; i++) {
  197.             if(adjacent(node, i)==1){
  198.                 if (!visited[i])
  199.                     dfsRecursive(i, visited);
  200.             }
  201.         }
  202.     }
  203.  
  204.     void dfsNonrecursive(int node) {
  205.         boolean visited[] = new boolean[num_nodes];
  206.         for (int i = 0; i < num_nodes; i++)
  207.             visited[i] = false;
  208.         visited[node] = true;
  209.         System.out.println(node + ": " + nodes[node]);
  210.         Stack<Integer> s = new Stack<Integer>();
  211.         s.push(node);
  212.  
  213.         int pom;
  214.  
  215.         while (!s.isEmpty()) {
  216.             pom = s.peek();
  217.             int pom1 = pom;
  218.             for (int i = 0; i < num_nodes; i++) {
  219.                 if(adjacent(pom,i)==1){
  220.                     pom1 = i;
  221.                     if(!visited[i])
  222.                         break;
  223.                 }
  224.             }
  225.             if(!visited[pom1]){
  226.                 visited[pom1] = true;
  227.                 System.out.println(pom1 + ": " + nodes[pom1]);
  228.                 s.push(pom1);
  229.             }
  230.             else
  231.                 s.pop();
  232.         }
  233.  
  234.     }
  235.  
  236.     void bfs(int node){
  237.         boolean visited[] = new boolean[num_nodes];
  238.         for (int i = 0; i < num_nodes; i++)
  239.             visited[i] = false;
  240.         visited[node] = true;
  241.         System.out.println(node+": " + nodes[node]);
  242.         Queue<Integer> q = new LinkedQueue<Integer>();
  243.         q.enqueue(node);
  244.  
  245.         int pom;
  246.  
  247.         while(!q.isEmpty()){
  248.             pom = q.dequeue();
  249.             for (int i = 0; i < num_nodes; i++) {
  250.                 if(adjacent(pom, i)==1){
  251.                     if (!visited[i]){
  252.                         visited[i] = true;
  253.                         System.out.println(i+": " + nodes[i]);
  254.                         q.enqueue(i);
  255.                     }
  256.  
  257.                 }
  258.             }
  259.  
  260.  
  261.         }
  262.  
  263.     }
  264.  
  265.  
  266.  
  267.     @Override
  268.     public String toString() {
  269.         String ret="";
  270.  
  271.         for(int i=0;i<num_nodes;i++){
  272.  
  273.             for(int j=0;j<num_nodes;j++)
  274.                 ret+=adjMat[i][j]+" ";
  275.  
  276.             if( i != num_nodes-1 )
  277.                 ret+="\n";
  278.         }
  279.         return ret;
  280.     }
  281.  
  282.  
  283. }
  284.  
  285. public class GraphCreate {
  286.  
  287.     public static void main(String[] args) throws IOException {
  288.  
  289.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  290.  
  291.         String input = br.readLine();
  292.         int numCommands = Integer.parseInt(input); //only for 1st line
  293.  
  294.         String[] commands = new String[3];
  295.  
  296.         int var1, var2;
  297.         char node = 0;
  298.         Graph<Character> gr = null;
  299.  
  300.         for (int i = 0; i < numCommands; i++) { // all commands
  301.  
  302.             input = br.readLine(); // reads whole line
  303.             // divides input in commands[], commands[1] is an integer
  304.             commands = input.split(" ");
  305.  
  306.             if (commands[0].equals("CREATE")) { // creates graps
  307.  
  308.                 var1 = Integer.parseInt(commands[1]); // second part of command
  309.                 gr = new Graph<>(var1);// creates graph
  310.  
  311.                 for (int j = 0; j < var1; j++) { // makes nodes in alphabetical order
  312.  
  313.                     node = (char) (65 + j);
  314.                     gr.set_node_value( j, node );
  315.                 }
  316.             }
  317.             if (commands[0].equals("ADDEDGE")) { // adds edge between
  318.  
  319.                 var1 = Integer.parseInt(commands[1]); // second part of command
  320.                 var2 = Integer.parseInt(commands[2]); // 3rd part of command
  321.                 gr.addEdge(var1, var2);
  322.             }
  323.             if (commands[0].equals("DELETEEDGE")) { // adds edge between
  324.  
  325.                 var1 = Integer.parseInt(commands[1]); // second part of command
  326.                 var2 = Integer.parseInt(commands[2]); // 3rd part of command
  327.                 gr.deleteEdge(var1, var2);
  328.             }
  329.             if (commands[0].equals("ADJACENT")) { // adds edge between
  330.  
  331.                 var1 = Integer.parseInt(commands[1]); // second part of command
  332.                 var2 = Integer.parseInt(commands[2]); // 3rd part of command
  333.                 System.out.println( gr.adjacent(var1, var2) );
  334.             }
  335.             if (commands[0].equals("PRINTMATRIX")) {
  336.  
  337.                 System.out.println( gr.toString() );
  338.             }
  339.             if (commands[0].equals("PRINTNODE")) {
  340.  
  341.                 var1 = Integer.parseInt(commands[1]); // second part of command
  342.                 System.out.println( gr.get_node_value(var1) );
  343.             }
  344.         }
  345.     }
  346. }
Add Comment
Please, Sign In to add comment