Skip to content
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

Adiciona versão simplificada da DFS para achar diâmetro #105

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions Grafos/slides/arvores_diametro/codes/dp2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <bits/stdc++.h>

using namespace std;
using ii = pair<int, int>;

const int MAX { 2 * 1'00'000 + 1 };
vector<int> adj[MAX];
int to_leaf[MAX], max_length[MAX];

void dfs(int u, int p)
{
int ds1, ds2;
ds1 = ds2 = -1;

for (auto v : adj[u])
{
if (v == p)
continue;

if (ds1 < ds2) swap(ds1, ds2);

dfs(v, u);

ds2 = max(ds2, to_leaf[v]);
}


to_leaf[u] = max(ds1, ds2) + 1;
max_length[u] = 2 + ds1 + ds2;
}

int diameter(int root, int N)
{
dfs(root, 0);

int d = 0;

for (int u = 1; u <= N; ++u)
d = max(d, max_length[u]);

return d;
}

int main()
{
vector<ii> edges { ii(1, 7), ii(3, 7), ii(7, 4), ii(4, 2),
ii(4, 5), ii(5, 6) };

for (const auto& [u, v] : edges) {
adj[u].push_back(v);
adj[v].push_back(u);
}

// 4
cout << diameter(4, 7) << endl;

// 0 0 0 2 1 0 1
for (int u = 1; u <= 7; ++u)
cout << to_leaf[u] << (u == 7 ? '\n' : ' ');

// 0 0 0 4 1 0 2
for (int u = 1; u <= 7; ++u)
cout << max_length[u] << (u == 7 ? '\n' : ' ');

return 0;
}
149 changes: 149 additions & 0 deletions Grafos/slides/arvores_diametro/main.tex
Original file line number Diff line number Diff line change
Expand Up @@ -1169,6 +1169,155 @@
\inputsnippet{cpp}{27}{41}{codes/dp.cpp}

\end{frame}

\begin{frame}[plain,t]
\begin{tikzpicture}
\node[draw,opacity=0] at (0, 0) {x};
\node[draw,opacity=0] at (14, 8) {x};

\node[anchor=west] (title) at (0.0, 7.0) { \Large \bbbold{Possível simplificação da DFS} };

\end{tikzpicture}
\end{frame}

\begin{frame}[plain,t]
\begin{tikzpicture}
\node[draw,opacity=0] at (0, 0) {x};
\node[draw,opacity=0] at (14, 8) {x};

\node[anchor=west] (title) at (0.0, 7.0) { \Large \bbbold{Possível simplificação da DFS} };
\node[anchor=west] (a) at (0.1, 6.0) { $\star$ \bbtext{Partindo da idéia anterior é possível guardar apenas os dois melhores valores. } };

\end{tikzpicture}
\end{frame}


\begin{frame}[plain,t]
\begin{tikzpicture}
\node[draw,opacity=0] at (0, 0) {x};
\node[draw,opacity=0] at (14, 8) {x};

\node[anchor=west] (title) at (0.0, 7.0) { \Large \bbbold{Possível simplificação da DFS} };
\node[anchor=west] (a) at (0.1, 6.0) { $\star$ \bbtext{Partindo da idéia anterior é possível guardar apenas os dois melhores valores. } };
\node[anchor=west] (a) at (0.1, 5.0) { $\star$ \bbtext{Guardaremos em $\displaystyle \mathrm{d1}$ e $\displaystyle \mathrm{d2}$, o maior e segundo maior valor para $\displaystyle \mathrm{to\_leaf}[v]$.} };

\end{tikzpicture}
\end{frame}

\begin{frame}[plain,t]
\begin{tikzpicture}
\node[draw,opacity=0] at (0, 0) {x};
\node[draw,opacity=0] at (14, 8) {x};
\node[anchor=west] (title) at (0.0, 7.0) { \Large \bbbold{Possível simplificação da DFS} };

\node[anchor=west] (a) at (0.1, 6.0) { $\star$ \bbtext{ Fazendo com que $\displaystyle \mathrm{to\_leaf}[u] = max(\displaystyle \mathrm{d1}, \displaystyle \mathrm{d2}) + 1$ tem-se que :} };

\end{tikzpicture}
\end{frame}

\begin{frame}[plain,t]
\begin{tikzpicture}
\node[draw,opacity=0] at (0, 0) {x};
\node[draw,opacity=0] at (14, 8) {x};
\node[anchor=west] (title) at (0.0, 7.0) { \Large \bbbold{Possível simplificação da DFS} };

\node[anchor=west] (a) at (0.1, 6.0) { $\star$ \bbtext{ Fazendo com que $\displaystyle \mathrm{to\_leaf}[u] = max(\displaystyle \mathrm{d1}, \displaystyle \mathrm{d2}) + 1$ tem-se que :} };
\node[anchor=west] (a) at (0.5, 5.5) { $\star$ \bbtext{Se $u$ for uma folha: $\displaystyle \mathrm{to\_leaf}[u] = max(\displaystyle \mathrm{d1}, \displaystyle \mathrm{d2}) + 1 = -1 + 1= 0$} };

\end{tikzpicture}
\end{frame}

\begin{frame}[plain,t]
\begin{tikzpicture}
\node[draw,opacity=0] at (0, 0) {x};
\node[draw,opacity=0] at (14, 8) {x};
\node[anchor=west] (title) at (0.0, 7.0) { \Large \bbbold{Possível simplificação da DFS} };

\node[anchor=west] (a) at (0.1, 6.0) { $\star$ \bbtext{ Fazendo com que $\displaystyle \mathrm{to\_leaf}[u] = max(\displaystyle \mathrm{d1}, \displaystyle \mathrm{d2}) + 1$ tem-se que :} };
\node[anchor=west] (a) at (0.5, 5.5) { $\star$ \bbtext{Se $u$ for uma folha: $\displaystyle \mathrm{to\_leaf}[u] = max(\displaystyle \mathrm{d1}, \displaystyle \mathrm{d2}) + 1 = -1 + 1= 0$} };
\node[anchor=west] (a) at (0.5, 5.0) { $\star$ \bbtext{Caso contrário o maior valor será somado.} };

\end{tikzpicture}
\end{frame}

\begin{frame}[plain,t]
\begin{tikzpicture}
\node[draw,opacity=0] at (0, 0) {x};
\node[draw,opacity=0] at (14, 8) {x};
\node[anchor=west] (title) at (0.0, 7.0) { \Large \bbbold{Possível simplificação da DFS} };

\node[anchor=west] (a) at (0.1, 6.0) { $\star$ \bbtext{ Fazendo com que $\displaystyle \mathrm{to\_leaf}[u] = max(\displaystyle \mathrm{d1}, \displaystyle \mathrm{d2}) + 1$ tem-se que :} };
\node[anchor=west] (a) at (0.5, 5.5) { $\star$ \bbtext{Se $u$ for uma folha: $\displaystyle \mathrm{to\_leaf}[u] = max(\displaystyle \mathrm{d1}, \displaystyle \mathrm{d2}) + 1 = -1 + 1= 0$} };
\node[anchor=west] (a) at (0.5, 5.0) { $\star$ \bbtext{Caso contrário o maior valor será somado.} };


\node[anchor=west] (a) at (0.1, 4.0) { $\star$ \bbtext{ Fazendo com que $\displaystyle \mathrm{max\_lenght}[u] = 2 + \displaystyle \mathrm{d1} + \displaystyle \mathrm{d2}$ tem-se que :} };

\end{tikzpicture}
\end{frame}

\begin{frame}[plain,t]
\begin{tikzpicture}
\node[draw,opacity=0] at (0, 0) {x};
\node[draw,opacity=0] at (14, 8) {x};
\node[anchor=west] (title) at (0.0, 7.0) { \Large \bbbold{Possível simplificação da DFS} };

\node[anchor=west] (a) at (0.1, 6.0) { $\star$ \bbtext{ Fazendo com que $\displaystyle \mathrm{to\_leaf}[u] = max(\displaystyle \mathrm{d1}, \displaystyle \mathrm{d2}) + 1$ tem-se que :} };
\node[anchor=west] (a) at (0.5, 5.5) { $\star$ \bbtext{Se $u$ for uma folha: $\displaystyle \mathrm{to\_leaf}[u] = max(\displaystyle \mathrm{d1}, \displaystyle \mathrm{d2}) + 1 = -1 + 1= 0$} };
\node[anchor=west] (a) at (0.5, 5.0) { $\star$ \bbtext{Caso contrário o maior valor será somado.} };


\node[anchor=west] (a) at (0.1, 4.0) { $\star$ \bbtext{ Fazendo com que $\displaystyle \mathrm{max\_lenght}[u] = 2 + \displaystyle \mathrm{d1} + \displaystyle \mathrm{d2}$ tem-se que :} };
\node[anchor=west] (a) at (0.5, 3.5) { $\star$ \bbtext{Se $u$ tiver pelo menos dois filhos somará os dois maiores valores.} };

\end{tikzpicture}
\end{frame}

\begin{frame}[plain,t]
\begin{tikzpicture}
\node[draw,opacity=0] at (0, 0) {x};
\node[draw,opacity=0] at (14, 8) {x};
\node[anchor=west] (title) at (0.0, 7.0) { \Large \bbbold{Possível simplificação da DFS} };

\node[anchor=west] (a) at (0.1, 6.0) { $\star$ \bbtext{ Fazendo com que $\displaystyle \mathrm{to\_leaf}[u] = max(\displaystyle \mathrm{d1}, \displaystyle \mathrm{d2}) + 1$ tem-se que :} };
\node[anchor=west] (a) at (0.5, 5.5) { $\star$ \bbtext{Se $u$ for uma folha: $\displaystyle \mathrm{to\_leaf}[u] = max(\displaystyle \mathrm{d1}, \displaystyle \mathrm{d2}) + 1 = -1 + 1= 0$} };
\node[anchor=west] (a) at (0.5, 5.0) { $\star$ \bbtext{Caso contrário o maior valor será somado.} };


\node[anchor=west] (a) at (0.1, 4.0) { $\star$ \bbtext{ Fazendo com que $\displaystyle \mathrm{max\_lenght}[u] = 2 + \displaystyle \mathrm{d1} + \displaystyle \mathrm{d2}$ tem-se que :} };

\node[anchor=west] (a) at (0.5, 3.5) { $\star$ \bbtext{Se $u$ tiver pelo menos dois filhos somará os dois maiores valores.} };
\node[anchor=west] (a) at (0.5, 3.0) { $\star$ \bbtext{Se $u$ apenas um filho: $\displaystyle \mathrm{max\_lenght}[u] = 2 + \displaystyle \mathrm{d1} + \displaystyle \mathrm{d2} = 2 + d1 + (-1) = 1 + d1$} };

\end{tikzpicture}
\end{frame}
\begin{frame}[plain,t]

\begin{tikzpicture}
\node[draw,opacity=0] at (0, 0) {x};
\node[draw,opacity=0] at (14, 8) {x};
\node[anchor=west] (title) at (0.0, 7.0) { \Large \bbbold{Possível simplificação da DFS} };

\node[anchor=west] (a) at (0.1, 6.0) { $\star$ \bbtext{ Fazendo com que $\displaystyle \mathrm{to\_leaf}[u] = max(\displaystyle \mathrm{d1}, \displaystyle \mathrm{d2}) + 1$ tem-se que :} };
\node[anchor=west] (a) at (0.5, 5.5) { $\star$ \bbtext{Se $u$ for uma folha: $\displaystyle \mathrm{to\_leaf}[u] = max(\displaystyle \mathrm{d1}, \displaystyle \mathrm{d2}) + 1 = -1 + 1= 0$} };
\node[anchor=west] (a) at (0.5, 5.0) { $\star$ \bbtext{Caso contrário o maior valor será somado.} };


\node[anchor=west] (a) at (0.1, 4.0) { $\star$ \bbtext{ Fazendo com que $\displaystyle \mathrm{max\_lenght}[u] = 2 + \displaystyle \mathrm{d1} + \displaystyle \mathrm{d2}$ tem-se que :} };

\node[anchor=west] (a) at (0.5, 3.5) { $\star$ \bbtext{Se $u$ tiver pelo menos dois filhos somará os dois maiores.} };
\node[anchor=west] (a) at (0.5, 3.0) { $\star$ \bbtext{Se $u$ apenas um filho: $\displaystyle \mathrm{max\_lenght}[u] = 2 + \displaystyle \mathrm{d1} + \displaystyle \mathrm{d2} = 2 + d1 + -1 = 1 + d1$} };
\node[anchor=west] (a) at (0.5, 2.5) { $\star$ \bbtext{Se $u$ for uma folha: $\displaystyle \mathrm{max\_lenght}[u] = 2 + \displaystyle \mathrm{d1} + \displaystyle \mathrm{d2} = 2 + -1 + -1 = 0$} };

\end{tikzpicture}
\end{frame}

\begin{frame}[plain,t]

\inputsnippet{cpp}{10}{30}{codes/dp2.cpp}

\end{frame}

\begin{frame}[plain,t]
\begin{tikzpicture}
\node[draw,opacity=0] at (0, 0) {x};
Expand Down