Advertisement
kish-dev

контрольная №2

Dec 8th, 2020
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.44 KB | None | 0 0
  1. //1  
  2.  Value delete(Key key) {
  3.         если root null
  4.                 вернуть null;
  5.         node prev := root;
  6.         node current := prev;
  7.         пока current.key <> key || current = null
  8.                 если key < current.key
  9.                     prev := current;
  10.                     current := current.left;
  11.                 если key > current.key
  12.                     prev := current;
  13.                     current := current.right;
  14.  
  15.         если current.key = key
  16.              проверяем не лист ли он
  17.                  если лист, просто удаляем
  18.                  обновляем указатель у предыдущего на текущий, что он null
  19.              иначе если у него есть правые потомки
  20.                   доходим до минимального правого(один раз вправо и далее влево до упора)
  21.                   меняем местами текущий и минимальный, удаляем минимальный, предварительно сохранив значение
  22.                   обновляем указатель у предыдущего на текущий
  23.                   вернуть value
  24.              иначе
  25.                    доходим до максимального левого
  26.                    меняем местами текущий и максимальный и удаляем его, сохранив в переменную
  27.                    обновляем указатель у предыдущего на текущий
  28.                    вернуть value
  29.         если не нашли key
  30.                 вернуть null
  31.     }
  32.  
  33.  
  34. //2
  35.    Node rotateLeft(Node y) {
  36.         Node x = y.right;
  37.         y.right = x.left;
  38.         x.left = y;
  39.         fixHeight(y);
  40.         fixHeight(x);
  41.         return x;
  42.     }
  43.  
  44.  
  45.  
  46. //3
  47.     void rehash() {
  48.         if (loadFactor() > loadFactor) {
  49.             int oldM = m;
  50.             List<Key, Value>[] prevTable = hashTable;
  51.             n = 0;
  52.             m *= 2;
  53.             hashTable = new List[m];
  54.             for (int i = 0; i < m; ++i) {
  55.                 hashTable[i] = new List<>();
  56.             }
  57.             for (int i = 0; i < oldM; ++i) {
  58.                 if (prevTable[i] != null) {
  59.                     Node<Key, Value> temp = prevTable[i].head;
  60.                     while (temp != null) {
  61.                         this.put(temp.key, temp.value);
  62.                         temp = temp.next;
  63.                     }
  64.                 }
  65.             }
  66.         }
  67.     }
  68.  
  69.     void put(Key key, Value value) {
  70.         if(loadFactor > 0.75)
  71.                 rehash();
  72.         int hash = hashCode(key);
  73.         int index = index(hash);
  74.         hashTable[index].add(key, value);
  75.     }
  76.  
  77.     void add (Key key, Value value) {
  78.         if (head == null) {
  79.             head = new Node<>(key, value, null);
  80.             return;
  81.         } else if (head.next == null) {
  82.             if (head.key.equals(key)) {
  83.                 head.value = value;
  84.                 return;
  85.             } else {
  86.                 head.next = new Node<>(key, value, null);
  87.                 return;
  88.             }
  89.         } else {
  90.             Node temp = head;
  91.             while (temp != null) {
  92.                 if (temp.key.equals(key)) {
  93.                     temp.value = value;
  94.                     return;
  95.                 }
  96.                 temp = temp.next;
  97.             }
  98.  
  99.             temp = head;
  100.             head = new Node<>(key, value, temp);
  101.             return;
  102.         }
  103.     }
  104.  
  105.  
  106.  
  107.  
  108. //4
  109.     void haveCycle(int v) {
  110.         colors[v] = 1;
  111.         for (int n : vertexes[v]) {
  112.             if(colors[n] == 0) {
  113.                 haveCycle(n);
  114.             }
  115.             if(colors[n] == 1) {
  116.                 this.haveCycles = true;
  117.                 return;
  118.             }
  119.         }
  120.         colors[v] = 2;
  121.         return;
  122.     }
  123.  
  124.     boolean isLinked() {
  125.         for(int v : colors) {
  126.             if(v == 0)
  127.                 return false;
  128.         }
  129.         return true;
  130.     }
  131.  
  132.  
  133.  
  134.  
  135.  
  136. //5
  137.     void floyd() {
  138.         for (int i=0; i<n; ++i)
  139.             for (int u=0; u<n; ++u)
  140.                 for (int v=0; v<n; ++v)
  141.                     if (d[u][i] < Double.POSITIVE_INFINITY && d[i][v] < Double.POSITIVE_INFINITY)
  142.                         d[u][v] = min (d[u][v], d[u][i] + d[i][v]);
  143.     }
  144.  
  145.  
  146.  
  147.  
  148.  
  149. //6
  150. package com.company;
  151. import java.util.*;
  152.  
  153. class Graph {
  154.     private final int countOfVertexes;
  155.     private final HashSet<Integer>[] vertexes;
  156.  
  157.     Graph(int v) {
  158.         countOfVertexes = v;
  159.         vertexes = new HashSet[v];
  160.         for (int i = 0; i < v; ++i)
  161.             vertexes[i] = new HashSet();
  162.     }
  163.  
  164.     //добавляем ребро в граф
  165.     void addEdge(int root, int outGoing) {
  166.         vertexes[root].add(outGoing);
  167.     }
  168.  
  169.     Graph reverse() {
  170.         Graph graphReverse = new Graph(countOfVertexes);
  171.  
  172.         for (int i = 0; i < countOfVertexes; ++i) {
  173.             for (int root : this.vertexes[i]) {
  174.                 graphReverse.vertexes[root].add(i);
  175.             }
  176.         }
  177.         return graphReverse;
  178.     }
  179.  
  180.     void reverseVisit(LinkedList<Integer> list, int v, boolean[] visited) {
  181.         visited[v] = true;
  182.  
  183.         for (int n : vertexes[v]) {
  184.             if (!visited[n]) {
  185.                 reverseVisit(list, n, visited);
  186.             }
  187.         }
  188.         list.addFirst(v);
  189.     }
  190.  
  191.     void DFSReverse(LinkedList<Integer> list) {
  192.         boolean[] visited = new boolean[countOfVertexes];
  193.  
  194.         for (int i = 0; i < countOfVertexes; ++i) {
  195.             if (!visited[i]) {
  196.                 reverseVisit(list, i, visited);
  197.             }
  198.         }
  199.     }
  200.  
  201.     void preVisitGraph(int v, int[] numSCC, int curSCC) {
  202.         numSCC[v] = curSCC;
  203.     }
  204.  
  205.     void visit(int v, int[] numSCC, int curSCC, boolean[] visited) {
  206.         visited[v] = true;
  207.         preVisitGraph(v, numSCC, curSCC);
  208.  
  209.         for (int n : vertexes[v]) {
  210.             if (!visited[n]) {
  211.                 visit(n, numSCC, curSCC, visited);
  212.             }
  213.         }
  214.     }
  215.  
  216.     void DFS(LinkedList<Integer> list, int[] numSCC) {
  217.         int curSCC = 0;
  218.         boolean[] visited = new boolean[countOfVertexes];
  219.         for (int n : list) {
  220.             if (!visited[n]) {
  221.                 visit(n, numSCC, curSCC++, visited);
  222.             }
  223.         }
  224.     }
  225.  
  226.     Graph getMetaGraph(int[] numSCC, int countSCC) {
  227.         Graph metaGraph = new Graph(countSCC);
  228.         for (int i = 0; i < vertexes.length; ++i) {
  229.             for (int vertex : vertexes[i]) {
  230.                 if (numSCC[i] != numSCC[vertex]) {
  231.                     //добавляем и возвращаем true, если нет элемента, если элемент есть,
  232.                     //возвращает false
  233.                     metaGraph.vertexes[numSCC[i]].add(numSCC[vertex]);
  234.                 }
  235.             }
  236.         }
  237.         return metaGraph;
  238.     }
  239.  
  240.     static void printSCC(LinkedList<Integer>[] arrayOfListsNumSCC) {
  241.         for (int i = 0; i < arrayOfListsNumSCC.length; ++i) {
  242.             Iterator<Integer> j = arrayOfListsNumSCC[i].listIterator();
  243.             System.out.print("SCC " + i + ": ");
  244.             while (j.hasNext()) {
  245.                 int vertex = j.next();
  246.                 System.out.print(vertex + " ");
  247.             }
  248.             System.out.println();
  249.         }
  250.     }
  251.  
  252.     void print() {
  253.         for (int i = 0; i < this.vertexes.length; ++i) {
  254.             Iterator<Integer> j = this.vertexes[i].iterator();
  255.             System.out.print(i + "   ");
  256.             while (j.hasNext()) {
  257.                 int vertex = j.next();
  258.                 System.out.print(vertex + " ");
  259.             }
  260.             System.out.println();
  261.         }
  262.     }
  263.  
  264.     public static void main(String[] args) {
  265.         Scanner scanner = new Scanner(System.in);
  266.         int n = scanner.nextInt();
  267.  
  268.         Graph graph = new Graph(n);
  269.  
  270.         for (int i = 0; i < n; ++i) {
  271.             int root = scanner.nextInt();
  272.             int outGoingVertex = scanner.nextInt();
  273.             while (outGoingVertex != -1) {
  274.                 graph.addEdge(root, outGoingVertex);
  275.                 outGoingVertex = scanner.nextInt();
  276.             }
  277.         }
  278.         Graph graphReverse = graph.reverse();
  279.  
  280.         //список всех вершин в порядке убывания их post-значений
  281.         LinkedList<Integer> list = new LinkedList<>();
  282.  
  283.         graphReverse.DFSReverse(list);
  284.  
  285.         int[] numSCC = new int[n];
  286.  
  287.         graph.DFS(list, numSCC);
  288.  
  289.         int max = Integer.MIN_VALUE;
  290.         for (int j : numSCC) {
  291.             if (j > max) {
  292.                 max = j;
  293.             }
  294.         }
  295.         //количество SCC
  296.         int countSCC = max + 1;
  297.  
  298.         //массив SCC со списком входящих в каждую SCC вершин
  299.         LinkedList<Integer>[] arrayOfListsNumSCC = new LinkedList[countSCC];
  300.  
  301.         for (int i = 0; i < arrayOfListsNumSCC.length; ++i) {
  302.             arrayOfListsNumSCC[i] = new LinkedList();
  303.         }
  304.         for (int i = 0; i < numSCC.length; ++i) {
  305.             arrayOfListsNumSCC[numSCC[i]].add(i);
  306.         }
  307.  
  308.         printSCC(arrayOfListsNumSCC);
  309.  
  310.         Graph metaGraph = graph.getMetaGraph(numSCC, countSCC);
  311.  
  312.         System.out.println("Meta-graph:");
  313.  
  314.         metaGraph.print();
  315.     }
  316. }
  317.  
  318.  
  319.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement