-
Notifications
You must be signed in to change notification settings - Fork 17
Kulaga Grisha #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Climentorii
wants to merge
8
commits into
DafeMipt213:main
Choose a base branch
from
Climentorii:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Kulaga Grisha #21
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5a88e74
4 points from first paragraph
Climentorii 6d4054a
Сделал 1, 4, 5 задачу с тестами
Climentorii d132907
task 1, 4, 5
Climentorii dda2ed5
Fixed clang format 1, 4, 5
Climentorii 29f52fc
Nothing in 2, 3 yet
Climentorii 53a5e3e
Added 2, 6 with tests
Climentorii e918862
Fixed clang in 2 task
Climentorii 6c64a47
Addded 3 with tests
Climentorii File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| <font size = 3> | ||
|
|
||
| ### 1.1. Ориентированный граф, псевдограф. Неориентированный граф, псевдограф. Связность в неор. графе, компоненты связности. Слабая и сильная связность в ор. графе. Компоненты слабой, сильной связности. | ||
| $\textbf{1.1.1 Граф}$ - упорядоченная пара $G$($V$, $E$), где $V$ - множество вершин, а $E$ $\subset$ ($V$ x $V$) - ребра. \ | ||
| \ | ||
| $\textbf{1.1.2 Ориентированный граф}$ - граф, в котором у каждого ребра есть направление, то есть стартовая и конечная вершины. \ | ||
| \ | ||
| $\textbf{1.1.3 Мультиграф}$ - граф, в котором хотя бы одна пара вершин соединена более чем одним ребром. \ | ||
| \ | ||
| $\textbf{1.1.4 Псевдограф}$ Псевдограф - мультиграф, в котором есть петли. \ | ||
| \ | ||
| $\textbf{1.1.5}$ Две вершины $u$ и $v$ **достижимы**, если найдется такая цепь из вершин $v$, $v_1$, $v_2$, ..., $v_n$, $u$, что $v_i$ смежна с $v_{i+1}$, а также смежны $v$, $v_1$ и $v_n$, $u$. \ | ||
| \ | ||
| $\textbf{1.1.6}$ Неориентированный граф **связен**, если для любой вершины все остальные достижимы из нее.\ | ||
| \ | ||
| $\textbf{1.1.7 Компонента связности в графе}$ - множество вершин графа достижимых попарно и рёбра их связывающие.\ | ||
| \ | ||
| $\textbf{1.1.8 Слабая связность в ориентированном графе}$ - ориентированный граф не связен, но его неориентированная копия связна.\ | ||
| \ | ||
| $\textbf{1.1.9 Сильная связность в оринетированном графе}$ - достижимость из произвольной вершины графа в любую другую.\ | ||
| \ | ||
| $\textbf{1.1.10 Компонента сильной связности в графе G}$ - подграф $G'$, который сильно связан.\ | ||
| \ | ||
| $\textbf{1.1.11 Компонента слабой связности в графе G}$ - подграф $G'$, который слабо связан. \ | ||
| ### 1.2. Обход в глубину. Цвета вершин. Времена входа и выхода. Лемма о белых путях(с доказательством). | ||
| \ | ||
| $\textbf{1.2.1 Обход в глубину (DFS)}$ - это рекурсивный алгоритм по поиску всех вершин графа или дерева. Обход подразумевает под собой посещение всех вершин графа.\ | ||
| \ | ||
| $\quad$ **Алгоритм:** \ | ||
| $\quad$ $\quad$ $1.$ Выбираем любую вершину $v$ из еще не посещенных. \ | ||
| $\quad$ $\quad$ $2.$ Помечаем ее как пройденную. \ | ||
| $\quad$ $\quad$ $3.$ Повторяем первые два шага к смежным к $v$ вершинам. \ | ||
| \ | ||
| $\textbf{1.2.2 Цвета вершин в DFS}$ \ | ||
| $\quad$ **Белый** - вершина еще не была посещена. \ | ||
| $\quad$ **Серый** - вершина в процессе обхода. \ | ||
| $\quad$ **Черный** - вершина, у которой посещены все смежные ей вершины. \ | ||
| \ | ||
| $\textbf{1.2.3 Времена входа и выхода в DFS}$ - назовем временами входа и выхода пару чисел $entry[u], leave[u]$. Массивы $leave$ и $entry$ будем заполнять в ходе модифицированного DFS. \ | ||
| \ | ||
| $\textbf{1.2.4 Модифицированный DFS.}$ | ||
| \ | ||
| $\quad$ **Алгоритм:** \ | ||
| $\quad$ $\quad$ $1.$ Все вершины в начале алгоритма белые. \ | ||
| $\quad$ $\quad$ $2.$ Заводим переменную $time = $ 0. \ | ||
| $\quad$ $\quad$ $3.$ Выберем произвольную вершину $u$, $entry[u] = time$. \ | ||
| $\quad$ $\quad$ $4.$ Красим вершину $u$ в серый цвет, инкрементируем $time$. \ | ||
| $\quad$ $\quad$ $5.$ Для каждой белой вершины, смежной с $u$, запускаем DFS. \ | ||
| $\quad$ $\quad$ $6.$ Красим $u$ в черный цвет, инкременитруем $time$, $leave[u] = time$. \ | ||
| \ | ||
| $\textbf{1.2.5 Лемма о белых путях.}$ \ | ||
| $\quad$ Пусть дан граф $G$. Запустим DFS($G$). Остановим выполнение процедуры DFS от какой-то вершины $u$ в тот момент, когда вершина $u$ была выкрашена в серый цвет (назовем первым моментом времени). Заметим, что в данный момент в графе $G$ есть как белые, так и черные и серые вершины. Продолжим выполнение процедуры DFS($u$) до того момента, когда вершина $u$ станет черной (второй момент времени). Тогда вершины графа $G$ \ {$u$}, бывшие черными и серыми в первый момент времени, не поменяют свой цвет ко второму моменту времени, а белые вершины либо останутся белыми, либо станут черными, причем черными станут те, что были достижимы от вершины $u$ по белым путям. \ | ||
|
|
||
| **Доказательство:** \ | ||
| $\quad$ Черные вершины останутся черными, потому что цвет может меняться только | ||
| по схеме белый, серый, черный. Серые останутся серыми, потому что они лежат в стеке рекурсии и | ||
| там и останутся. \ | ||
| \ | ||
| $\quad$ Заметим, что не существует такого момента в процессе обхода, что существует ребро из черной $v$ | ||
| вершины в белую $u$. Действительно, запустим DFS($v$). В этот момент $v$ стала серой, а 𝑢 была белой. | ||
| Далее будет запущен DFS($u$), так как $u$ была белой. По алгоритму вершина $v$ будет покрашена в | ||
| черный цвет тогда, когда завершится обход всех вершин, достижимых из нее по одному ребру, кроме | ||
| тех, что были рассмотрены раньше нее. Таким образом, вершина $v$ может стать черной только тогда, | ||
| когда DFS выйдет из вершины $u$, и она будет покрашена в черный цвет. Получаем противоречие. \ | ||
| \ | ||
| $\quad$ Теперь заметим, что если вершина достижима по пути из белых вершин в первый момент времени, | ||
| то она стала черной ко сторому моменту времени (из абзаца выше следует). | ||
| Заметим, что это верно и в обратную сторону. Рассмотрим момент, когда вершина $v$ стала черной: | ||
| в этот момент существует cерый путь из $u$ в $v$, а это значит, что в первый момент времени сущестовал | ||
| белый путь из $u$ в $v$. \ | ||
| \ | ||
| $\quad$ Отсюда следует, что если вершина была перекрашена из белой в черную, то она была достижима | ||
| по белому пути, и что если вершина как была, так и осталась белой, она не была достижима по | ||
| белому пути, что и требовалось доказать. \ | ||
| ### 1.3. Проверка связности неориентированного графа. Поиск цикла в неориентированном и ориентированном графе. Топологическая сортировка. | ||
| $\textbf{1.3.1 Проверка связности ориентированного графа.}$ - модифицируем DFS так, чтобы он возвращал число посещенных вершин. Тогда запустим его от произвольной вершины, и если возвращенное число равно числу вершин в графе, то граф связный. \ | ||
| \ | ||
| $\textbf{1.3.2 Поиск цикла в неориентированном и ориентированном графе.}$ \ | ||
| $\quad$ Будем решать задачу с помощью поиска в глубину. \ | ||
| \ | ||
| $\quad$ В случае ориентированного графа произведём серию обходов. То есть из каждой вершины, в | ||
| которую мы ещё ни разу не приходили, запустим поиск в глубину, который при входе в вершину | ||
| будет красить её в серый цвет, а при выходе из нее — в чёрный. И, если алгоритм пытается пойти в | ||
| серую вершину, то это означает, что цикл найден. \ | ||
| \ | ||
| $\quad$ В случае неориентированного графа, одно ребро не должно встречаться в цикле дважды по определению. Поэтому необходимо дополнительно проверять, что текущее рассматриваемое из вершины | ||
| ребро не является тем ребром, по которому мы пришли в эту вершину. \ | ||
| \ | ||
| $\textbf{1.3.3 Топологическая сортировка.}$ \ | ||
| $\quad$ Топологическая сортировка ориентированного ациклического графа $G(V, E)$ представляет собой | ||
| упорядочивание вершин таким образом, что для любого ребра ($u$, $v$) $\in$ $E$ номер вершины 𝑢 меньше | ||
| номера вершины $u$. \ | ||
| \ | ||
| $\quad$ Предположим, что граф ацикличен, т.е. решение существует. Что делает обход в глубину? При | ||
| запуске из какой-то вершины $v$ он пытается запуститься вдоль всех рёбер, исходящих из $v$. Вдоль | ||
| тех рёбер, концы которых уже были посещены ранее, он не проходит, а вдоль всех остальных — | ||
| проходит и вызывает себя от их концов. \ | ||
| \ | ||
| $\quad$ Таким образом, к моменту выхода из вызова DFS($v$) все вершины, достижимые из $v$ как непосредственно (по одному ребру), так и косвенно (по пути)— все такие вершины уже посещены обходом. Следовательно, если мы будем в момент выхода из DFS($v$) добавлять нашу вершину в начало некоего списка, то в конце концов в этом списке получится топологическая сортировка. \ | ||
| ### 1.4. Нахождение компонент сильной связности. Алгоритм Косарайю. Алгоритм Тарьяна. | ||
| \ | ||
| $\textbf{1.4.1 нахождение компонент сильной связности. Алгоритм Косарайю}$ \ | ||
| $\quad$ **Алгоритм :** \ | ||
| $\quad$ $\quad$ $1.$ Строим граф $H$ на основе данного графа $G$, инвертируя все ребра. \ | ||
| $\quad$ $\quad$ $2.$ Запускаем DFS на этом графе, вычисляющий для каждой вершины время выхода DFS из нее. Пусть эти данные будут находиться в массиве $outTime$. \ | ||
| $\quad$ $\quad$ $3.$ Выполняем DFS на исходном графе, перебирая вершины в порядке убывания $outTime[u]$. \ | ||
| \ | ||
| Подробнее про работу алгоритма рассказывается в этом видео: https://www.youtube.com/watch?v=-UgiBh1IMQU&t=704s \ | ||
| \ | ||
|
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,21 @@ | ||
| #include <algorithm> | ||
| #include <iomanip> | ||
| #include <iostream> | ||
| #include <vector> | ||
|
|
||
| int main() { return 0; } | ||
| #include "topology_sort.hpp" | ||
|
|
||
| int main() { | ||
| int n, m; | ||
| std::cin >> n >> m; | ||
|
|
||
| std::vector<std::vector<int> > graph; | ||
| graph.resize(n); | ||
| int tmp_from, tmp_to; | ||
| for (int i = 0; i < m; ++i) { | ||
| std::cin >> tmp_from >> tmp_to; | ||
| graph[tmp_from].push_back(tmp_to); | ||
| } | ||
|
|
||
| std::vector<int> vec = top_sort(graph); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1 +1,28 @@ | ||||||
| #include "topology_sort.hpp" | ||||||
|
|
||||||
| #include <algorithm> | ||||||
|
|
||||||
| void dfs(int v, std::vector<bool> &used, std::vector<int> &result, | ||||||
| std::vector<std::vector<int> > &graph) { | ||||||
| used[v] = true; | ||||||
| for (int i = 0; i < graph[v].size(); ++i) { | ||||||
| int to = graph[v][i]; | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: variable 'to' of type 'int' can be declared 'const' [misc-const-correctness]
Suggested change
|
||||||
| if (!used[to]) dfs(to, used, result, graph); | ||||||
| } | ||||||
|
|
||||||
| result.push_back(v); | ||||||
| } | ||||||
|
|
||||||
| std::vector<int> top_sort(std::vector<std::vector<int> > &graph) { | ||||||
| std::vector<bool> used; | ||||||
| used.resize(graph.size()); | ||||||
|
|
||||||
| std::vector<int> result; | ||||||
| for (int i = 0; i < graph.size(); ++i) { | ||||||
| if (!used[i]) dfs(i, used, result, graph); | ||||||
| } | ||||||
|
|
||||||
| reverse(result.begin(), result.end()); | ||||||
|
|
||||||
| return result; | ||||||
| } | ||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,7 @@ | ||
| #pragma once | ||
| #include <vector> | ||
|
|
||
| void dfs(int v, std::vector<bool> &used, std::vector<int> &result, | ||
| std::vector<std::vector<int> > &graph); | ||
|
|
||
| std::vector<int> top_sort(std::vector<std::vector<int> > &graph); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,25 @@ | ||||||
| #include "AllFunc.hpp" | ||||||
|
|
||||||
| void AllFunc(std::vector<std::vector<int> > &graph, | ||||||
| std::set<std::pair<int, int> > &result, std::set<int> &cpvector) { | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: parameter 'cpvector' is unused [misc-unused-parameters]
Suggested change
|
||||||
| int timer = 0, parent = -1; | ||||||
|
|
||||||
| std::vector<int> tin(graph.size(), 0); | ||||||
| std::vector<int> fup(graph.size(), 0); | ||||||
| std::vector<bool> used(graph.size(), 0); | ||||||
|
|
||||||
| std::set<std::pair<int, int> > res; | ||||||
|
|
||||||
| std::set<int> cpvec; | ||||||
|
|
||||||
| for (int i = 0; i < graph.size(); i++) { | ||||||
| if (!used[i]) { | ||||||
| FindBridges(timer, i, graph, tin, fup, parent, res, used); | ||||||
| } | ||||||
| } | ||||||
| result = returnBridges(res); | ||||||
| tin.resize(graph.size(), 0); | ||||||
| fup.resize(graph.size(), 0); | ||||||
| used.resize(graph.size(), 0); | ||||||
| FindCutPoint(timer, 0, graph, tin, fup, parent, cpvec, used); | ||||||
| } | ||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| #pragma once | ||
|
|
||
| #include "FindBridges.hpp" | ||
| #include "FindCutPoints.hpp" | ||
|
|
||
| void AllFunc(std::vector<std::vector<int> > &graph, | ||
| std::set<std::pair<int, int> > &result, std::set<int> &cpvector); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| #include "FindBridges.hpp" | ||
|
|
||
| void FindBridges(int &timer, int start, std::vector<std::vector<int> > &graph, | ||
| std::vector<int> &tin, std::vector<int> &fup, int parent, | ||
| std::set<std::pair<int, int> > &result, | ||
| std::vector<bool> used) { | ||
| used[start] = 1; | ||
| tin[start] = fup[start] = ++timer; | ||
| int to; | ||
| for (int i = 0; i < graph[start].size(); ++i) { | ||
| to = graph[start][i]; | ||
| if (to == parent) continue; | ||
| if (used[to]) | ||
| fup[start] = std::min(fup[start], tin[to]); | ||
| else { | ||
| FindBridges(timer, to, graph, tin, fup, start, result, used); | ||
| fup[start] = std::min(fup[start], fup[to]); | ||
| if (fup[to] > tin[start]) | ||
| result.insert({std::min(start, to), std::max(start, to)}); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| std::set<std::pair<int, int> > returnBridges( | ||
| std::set<std::pair<int, int> > &result) { | ||
| return result; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| #pragma once | ||
|
|
||
| #include <iostream> | ||
| #include <set> | ||
| #include <utility> | ||
| #include <vector> | ||
|
|
||
| void FindBridges(int &timer, int start, std::vector<std::vector<int> > &graph, | ||
| std::vector<int> &tin, std::vector<int> &fup, int parent, | ||
| std::set<std::pair<int, int> > &result, | ||
| std::vector<bool> used); | ||
|
|
||
| std::set<std::pair<int, int> > returnBridges( | ||
| std::set<std::pair<int, int> > &result); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| #include "FindCutPoints.hpp" | ||
|
|
||
| void FindCutPoint(int &timer, int start, std::vector<std::vector<int> > &graph, | ||
| std::vector<int> &tin, std::vector<int> &fup, int parent, | ||
| std::set<int> &cpvec, std::vector<bool> used) { | ||
| used[start] = 1; | ||
| tin[start] = fup[start] = ++timer; | ||
| int to; | ||
| int children = 0; | ||
| for (int i = 0; i < graph[start].size(); ++i) { | ||
| to = graph[start][i]; | ||
| if (to == parent) continue; | ||
| if (used[to]) | ||
| fup[start] = std::min(fup[start], tin[to]); | ||
| else { | ||
| FindCutPoint(timer, to, graph, tin, fup, start, cpvec, used); | ||
| fup[start] = std::min(fup[start], fup[to]); | ||
| if (fup[to] >= tin[start] && parent != -1) cpvec.insert(start); | ||
| children++; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| std::set<int> returnCP(std::set<int> &cpvec) { return cpvec; } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| #pragma once | ||
|
|
||
| #include <iostream> | ||
| #include <set> | ||
| #include <utility> | ||
| #include <vector> | ||
|
|
||
| void FindCutPoint(int &timer, int start, std::vector<std::vector<int> > &graph, | ||
| std::vector<int> &tin, std::vector<int> &fup, int parent, | ||
| std::set<int> &cpvec, std::vector<bool> used); | ||
|
|
||
| std::set<int> returnCP(std::set<int> &cpvec); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| #include <iostream> | ||
| #include "FindBridges.hpp" | ||
| #include "FindCutPoints.hpp" | ||
|
|
||
| int main() { return 0; } | ||
| int main() {} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
warning: variable 'vec' of type 'std::vector' can be declared 'const' [misc-const-correctness]