Advertisement
PJF16

Untitled

May 23rd, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.75 KB | None | 0 0
  1. package ad1.ss16.pa;
  2. import java.util.*;
  3. import java.util.logging.Level;
  4. import java.util.logging.Logger;
  5.  
  6. public class Network implements Cloneable{
  7.  
  8.     int currNodes = 0;
  9.     Node[] nodes;
  10.     boolean[] discovered; //ob Knoten schon entdeckt ist
  11.     int distances [];
  12.  
  13.     /* Konstruktor
  14.        Benoetigte Instanzvariablen initialisieren
  15.     */
  16.  
  17.     public Network(Network net) {
  18.         this.nodes = net.nodes;
  19.         this.discovered = net.discovered;
  20.         this.distances = net.distances;
  21.         this.numberOfComp = net.numberOfComp;
  22.         this.critical = net.critical;
  23.         this.countChild = net.countChild;
  24.     }
  25.    
  26.    
  27.     public Network(int n) {
  28.         this.nodes = new Node[n];
  29.         for (int i = 0; i < this.nodes.length; i++) {
  30.             this.nodes[i] = new Node();
  31.         }
  32.        
  33.         this.distances = new int[nodes.length];
  34.         this.discovered = new boolean[nodes.length];
  35.         this.critical = new LinkedList <Integer>();
  36.     }
  37.  
  38.     //einfach die Laenge zurueckgeben
  39.     public int numberOfNodes() {
  40.         return this.nodes.length;
  41.     }
  42.  
  43.     /*
  44.         Die Groesse der Adjazenzlisten addieren
  45.         Jede Connetions ist zwei Mal enthalten. Dadurch vor dem Zurueckgeben durch zwei addieren
  46.         Dies ergebit die Anzahl der Connections
  47.     */
  48.     public int numberOfConnections() {
  49.         int help = 0;
  50.         for (int i = 0; i < this.nodes.length; i++)
  51.             help = help + this.nodes[i].adjazenzList.size();
  52.        
  53.         help = help/2;
  54.         return help;
  55.     }
  56.    
  57.     /*
  58.         Parameter v und w darf natuerlich nicht gleich sein
  59.         Dann ueberpruefen ob die Connection eventuell schon besteht
  60.         Danach den jeweiligen Knoten in die jeweilige Adjazenzliste einfuegen
  61.     */
  62.     public void addConnection(int v, int w) {
  63.         if (v != w) {
  64.             if (!this.nodes[v].adjazenzList.contains(w)) {
  65.                 this.nodes[v].adjazenzList.add(w);
  66.                 this.nodes[w].adjazenzList.add(v);
  67.             }
  68.         }
  69.     }
  70.     /*
  71.         Nimmt addConnection (int v, intw) zur Hilfe
  72.     */
  73.     public void addAllConnections(int v) {
  74.         for (int i = 0; i < this.nodes.length; i++) {
  75.             this.addConnection (v, i);
  76.         }
  77.     }
  78.  
  79.     /*
  80.         Connection loeschen
  81.         Einfach in beiden Adjazenzlisten den jeweiligen Knoten entfernen
  82.     */
  83.     public void deleteConnection(int v, int w) {
  84.         this.nodes[v].adjazenzList.remove(w);
  85.         this.nodes[w].adjazenzList.remove(v);
  86.     }
  87.    
  88.     /*
  89.         Loescht so lange Knoten bis peek() null liefert
  90.         Zur Hilfe wird deleteConnection (int v, int w) genutzt
  91.     */
  92.     public void deleteAllConnections(int v) {
  93.         while (nodes[v].adjazenzList.peek() != null) {
  94.            this.deleteConnection(v, this.nodes[v].adjazenzList.peek());
  95.         }
  96.     }
  97.    
  98.    
  99.     /*
  100.    
  101.     */
  102.     int numberOfComp;
  103.    
  104.     public int numberOfComponents() {
  105.         Arrays.fill(this.discovered, false);
  106.        
  107.         this.numberOfComp =0;
  108.        
  109.         for (int i=0; i<this.nodes.length; i++){
  110.             if (!discovered[i]) {
  111.                 this.numberOfComp++;
  112.                 this.numberOfComponents(i);
  113.             }
  114.         }
  115.        
  116.         return this.numberOfComp;
  117.        
  118.     }
  119.      
  120.    
  121.     private void numberOfComponents(int z) {
  122.         this.discovered[z]=true;
  123.        
  124.         for (int n : this.nodes[z].adjazenzList) {
  125.             if (!this.discovered[n])
  126.                 this.numberOfComponents(n);
  127.         }
  128.     }
  129.    
  130.     /*
  131.         Wenn die Anzahl der Knoten kleiner sind als die Connections, muss ein Kreis existieren
  132.     */
  133.     public boolean hasCycle() {
  134.         int standalones = 0;
  135.        
  136.         for (Node n : this.nodes) {
  137.             if (n.adjazenzList.isEmpty() && n.adjazenzList != null)
  138.                 standalones ++;
  139.         }
  140.        
  141.         if (this.numberOfConnections() != 0 || !(this.numberOfComponents() >= this.numberOfNodes())) {
  142.             if ((this.numberOfNodes() - standalones - (this.numberOfComponents()-standalones-1)) <= this.numberOfConnections())
  143.                 return true;
  144.         }
  145.         return false;
  146.     }  
  147.    
  148.     public int minimalNumberOfConnections(int start, int end) {
  149.         // Wenn Startknoten gleich Endknoten ist, dann ist die Reichweite = 0
  150.         if (start == end) {
  151.             return 0;
  152.         }
  153.        
  154.         return this.shortestPath(0, -1, start, end);
  155.      
  156.        
  157.     }
  158.  
  159.     /*
  160.         Tarjan Algorithmus verwendet
  161.     */
  162.  
  163.     public List<Integer> criticalNodes() {
  164.  
  165.         this.critical = new LinkedList<Integer>();
  166.         Arrays.fill(this.discovered, false);
  167.         for (int i = 0; i < nodes.length; i++) {
  168.             if (!this.discovered[i]) {
  169.                 this.calculateCritical(i);
  170.             }
  171.         }
  172.         return this.critical;
  173.     }
  174.  
  175.     LinkedList<Integer> critical;
  176.     int time;
  177.  
  178.     public void calculateCritical(int start) {
  179.         this.time = 0;
  180.         this.nodes[start].parent = -1;
  181.         this.countChild = 0;
  182.         this.DFS(start);
  183.     }
  184.  
  185.     /*
  186.     LinkedList <Integer> critical;
  187.      public List<Integer> criticalNodes(){
  188.          int actualComponents = this.numberOfComponents();
  189.          critical = new LinkedList <Integer>();
  190.          
  191.          Network clone = new Network (this);
  192.          
  193.          
  194.          for (Node n : this.nodes) {
  195.              for (int s : n.adjazenzList) {
  196.                  clone.deleteAllConnections(s);
  197.                  if(actualComponents+2 <= clone.numberOfComponents())
  198.                      critical.add(s);
  199.              }
  200.          }
  201.          
  202.          
  203.          
  204.          
  205.          return critical;
  206.      }
  207. */
  208.    
  209.     /*
  210.        ***************************************************************************
  211.        *                        HELPER SECTION                                   *
  212.        ***************************************************************************
  213.     */
  214.    
  215.  
  216. /*    
  217.     Stack<Integer> tempstack;
  218.     Stack<Integer> auxStack;
  219.  
  220.     private void newDFS(int actualnode) {
  221.         this.tempstack = new Stack<Integer>();
  222.         this.discovered[actualnode] = true;
  223.         tempstack.push(actualnode);
  224.  
  225.         while (!tempstack.isEmpty()) {
  226.             int v = tempstack.pop();
  227.             if (!this.discovered[v]) {
  228.                 this.discovered[v] = true;
  229.  
  230.                 this.auxStack = new Stack<Integer>();
  231.                 for (int w : this.nodes[v].adjazenzList) {
  232.                     if (!this.discovered[w]) {
  233.                         this.auxStack.push(w);
  234.                     }
  235.                 }
  236.                 while (!auxStack.isEmpty()) {
  237.                     tempstack.push(auxStack.pop());
  238.                 }
  239.             }
  240.         }
  241.  
  242.     }*/
  243.    
  244.    
  245.     int countChild;
  246.  
  247.     private void DFS(int actualnode) {
  248.  
  249.         this.discovered[actualnode] = true;
  250.         this.nodes[actualnode].ltime = time++;
  251.         this.nodes[actualnode].time = time;
  252.  
  253.         if (this.nodes[actualnode].parent == -1) {
  254.             this.countChild = 0;
  255.         }
  256.         this.nodes[actualnode].isAP = false;
  257.  
  258.         Iterator tempIT = nodes[actualnode].adjazenzList.iterator();
  259.         while (tempIT.hasNext()) {
  260.             int adjazenz = (Integer) tempIT.next();
  261.  
  262.             if (adjazenz == this.nodes[actualnode].parent) {
  263.                 continue;
  264.             }
  265.  
  266.             if (!this.discovered[adjazenz]) {
  267.                 this.nodes[adjazenz].parent = actualnode;
  268.                 if (this.nodes[actualnode].parent == -1) {
  269.                     this.countChild++;
  270.                 }
  271.  
  272.                 this.DFS(adjazenz);
  273.  
  274.                 if (this.nodes[actualnode].time <= nodes[adjazenz].ltime) {
  275.                     this.nodes[actualnode].isAP = true;
  276.                 } else {
  277.                     this.nodes[actualnode].ltime = Math.min(nodes[actualnode].ltime, nodes[adjazenz].ltime);
  278.                 }
  279.             } else {
  280.                 this.nodes[actualnode].ltime = Math.min(nodes[actualnode].ltime, nodes[adjazenz].time);
  281.             }
  282.         }
  283.  
  284.         if (this.nodes[actualnode].parent == -1 && this.countChild >= 2 || this.nodes[actualnode].parent != -1 && this.nodes[actualnode].isAP) {
  285.             this.critical.add(actualnode);
  286.         }
  287.     }
  288.    
  289.  
  290.    
  291.     public int shortestPath(int level, int nextlevel, int start, int end) {
  292.         Arrays.fill(this.discovered, false);
  293.  
  294.         this.discovered[start] = true;
  295.  
  296.         Queue<Integer> q = new LinkedList<Integer>();
  297.  
  298.         q.offer(start);
  299.        
  300.         while (!q.isEmpty()) {
  301.             int help = q.poll();
  302.  
  303.             if (help == nextlevel) {
  304.                 nextlevel = -1;
  305.                 level++;
  306.             }
  307.  
  308.             if (help == end) {
  309.                 return level;
  310.             }
  311.  
  312.             for (int node : this.nodes[help].adjazenzList) {
  313.                 if (!discovered[node]) {
  314.                     discovered[node] = true;
  315.                     q.offer(node);
  316.  
  317.                     if (nextlevel == -1) {
  318.                         nextlevel = node;
  319.                     }
  320.                 }
  321.             }
  322.         }
  323.  
  324.         return -1;
  325.     }
  326.  
  327.     /*
  328.         Node Klasse - stellt ein Blatt dar.
  329.     */
  330. private class Node {
  331.  
  332.         public Queue<Integer> adjazenzList;  
  333.         int time;
  334.         int ltime;
  335.         int parent;
  336.         boolean isAP;
  337.  
  338.         public Node() {
  339.             this.adjazenzList = new PriorityQueue<Integer>();
  340.         }
  341.        
  342.         public void clone (Node n) {
  343.             this.adjazenzList = n.adjazenzList;
  344.             this.time = n.time;
  345.             this.ltime= n.ltime;
  346.             this.parent=n.parent;
  347.             this.isAP=n.isAP;
  348.         }
  349.        
  350.     }
  351. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement