Skip to content

Commit 09afe69

Browse files
committed
Redes - Grafos
1 parent c9700b3 commit 09afe69

File tree

5 files changed

+262
-0
lines changed

5 files changed

+262
-0
lines changed

digraphs.ipynb

Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 46,
6+
"metadata": {},
7+
"outputs": [
8+
{
9+
"name": "stdout",
10+
"output_type": "stream",
11+
"text": [
12+
"'A' --> 'G'\n",
13+
"Mayor ancho de banda: A > B > D > F > G -> 950mb\n",
14+
"El camino más corto: A > C > E > G -> 3 routers\n"
15+
]
16+
},
17+
{
18+
"data": {
19+
"image/svg+xml": [
20+
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
21+
"<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
22+
" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
23+
"<!-- Generated by graphviz version 2.50.0 (0)\n",
24+
" -->\n",
25+
"<!-- Pages: 1 -->\n",
26+
"<svg width=\"539pt\" height=\"153pt\"\n",
27+
" viewBox=\"0.00 0.00 539.00 153.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
28+
"<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 149)\">\n",
29+
"<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-149 535,-149 535,4 -4,4\"/>\n",
30+
"<!-- A -->\n",
31+
"<g id=\"node1\" class=\"node\">\n",
32+
"<title>A</title>\n",
33+
"<ellipse fill=\"none\" stroke=\"black\" cx=\"27\" cy=\"-51\" rx=\"27\" ry=\"18\"/>\n",
34+
"<text text-anchor=\"middle\" x=\"27\" y=\"-47.3\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">A</text>\n",
35+
"</g>\n",
36+
"<!-- B -->\n",
37+
"<g id=\"node2\" class=\"node\">\n",
38+
"<title>B</title>\n",
39+
"<ellipse fill=\"none\" stroke=\"black\" cx=\"138\" cy=\"-24\" rx=\"27\" ry=\"18\"/>\n",
40+
"<text text-anchor=\"middle\" x=\"138\" y=\"-20.3\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">B</text>\n",
41+
"</g>\n",
42+
"<!-- A&#45;&gt;B -->\n",
43+
"<g id=\"edge1\" class=\"edge\">\n",
44+
"<title>A&#45;&gt;B</title>\n",
45+
"<path fill=\"none\" stroke=\"black\" d=\"M51.61,-42.96C58.17,-40.87 65.33,-38.72 72,-37 81.49,-34.55 91.86,-32.29 101.48,-30.37\"/>\n",
46+
"<polygon fill=\"black\" stroke=\"black\" points=\"102.39,-33.75 111.54,-28.42 101.06,-26.88 102.39,-33.75\"/>\n",
47+
"<text text-anchor=\"middle\" x=\"82.5\" y=\"-40.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">200</text>\n",
48+
"</g>\n",
49+
"<!-- C -->\n",
50+
"<g id=\"node3\" class=\"node\">\n",
51+
"<title>C</title>\n",
52+
"<ellipse fill=\"none\" stroke=\"black\" cx=\"138\" cy=\"-81\" rx=\"27\" ry=\"18\"/>\n",
53+
"<text text-anchor=\"middle\" x=\"138\" y=\"-77.3\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">C</text>\n",
54+
"</g>\n",
55+
"<!-- A&#45;&gt;C -->\n",
56+
"<g id=\"edge2\" class=\"edge\">\n",
57+
"<title>A&#45;&gt;C</title>\n",
58+
"<path fill=\"none\" stroke=\"black\" d=\"M52.19,-57.66C67.15,-61.78 86.57,-67.12 103.06,-71.66\"/>\n",
59+
"<polygon fill=\"black\" stroke=\"black\" points=\"102.31,-75.08 112.88,-74.36 104.16,-68.33 102.31,-75.08\"/>\n",
60+
"<text text-anchor=\"middle\" x=\"82.5\" y=\"-71.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">100</text>\n",
61+
"</g>\n",
62+
"<!-- D -->\n",
63+
"<g id=\"node4\" class=\"node\">\n",
64+
"<title>D</title>\n",
65+
"<ellipse fill=\"none\" stroke=\"black\" cx=\"249\" cy=\"-24\" rx=\"27\" ry=\"18\"/>\n",
66+
"<text text-anchor=\"middle\" x=\"249\" y=\"-20.3\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">D</text>\n",
67+
"</g>\n",
68+
"<!-- B&#45;&gt;D -->\n",
69+
"<g id=\"edge3\" class=\"edge\">\n",
70+
"<title>B&#45;&gt;D</title>\n",
71+
"<path fill=\"none\" stroke=\"black\" d=\"M164.53,-19.92C170.57,-19.12 177,-18.41 183,-18 192.31,-17.37 194.69,-17.37 204,-18 206.72,-18.18 209.53,-18.43 212.34,-18.72\"/>\n",
72+
"<polygon fill=\"black\" stroke=\"black\" points=\"212.12,-22.22 222.47,-19.92 212.95,-15.27 212.12,-22.22\"/>\n",
73+
"<text text-anchor=\"middle\" x=\"193.5\" y=\"-21.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">300</text>\n",
74+
"</g>\n",
75+
"<!-- C&#45;&gt;D -->\n",
76+
"<g id=\"edge4\" class=\"edge\">\n",
77+
"<title>C&#45;&gt;D</title>\n",
78+
"<path fill=\"none\" stroke=\"black\" d=\"M159.01,-69.3C166.49,-65.02 175.08,-60.19 183,-56 194.09,-50.13 206.41,-43.97 217.31,-38.64\"/>\n",
79+
"<polygon fill=\"black\" stroke=\"black\" points=\"219,-41.72 226.47,-34.21 215.94,-35.42 219,-41.72\"/>\n",
80+
"<text text-anchor=\"middle\" x=\"193.5\" y=\"-59.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">250</text>\n",
81+
"</g>\n",
82+
"<!-- E -->\n",
83+
"<g id=\"node5\" class=\"node\">\n",
84+
"<title>E</title>\n",
85+
"<ellipse fill=\"none\" stroke=\"black\" cx=\"393\" cy=\"-72\" rx=\"27\" ry=\"18\"/>\n",
86+
"<text text-anchor=\"middle\" x=\"393\" y=\"-68.3\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">E</text>\n",
87+
"</g>\n",
88+
"<!-- C&#45;&gt;E -->\n",
89+
"<g id=\"edge5\" class=\"edge\">\n",
90+
"<title>C&#45;&gt;E</title>\n",
91+
"<path fill=\"none\" stroke=\"black\" d=\"M165.12,-80.07C210.6,-78.45 304.37,-75.12 355.98,-73.28\"/>\n",
92+
"<polygon fill=\"black\" stroke=\"black\" points=\"356.15,-76.78 366.02,-72.92 355.9,-69.78 356.15,-76.78\"/>\n",
93+
"<text text-anchor=\"middle\" x=\"249\" y=\"-80.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">200</text>\n",
94+
"</g>\n",
95+
"<!-- H -->\n",
96+
"<g id=\"node6\" class=\"node\">\n",
97+
"<title>H</title>\n",
98+
"<ellipse fill=\"none\" stroke=\"black\" cx=\"321\" cy=\"-127\" rx=\"27\" ry=\"18\"/>\n",
99+
"<text text-anchor=\"middle\" x=\"321\" y=\"-123.3\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">H</text>\n",
100+
"</g>\n",
101+
"<!-- C&#45;&gt;H -->\n",
102+
"<g id=\"edge6\" class=\"edge\">\n",
103+
"<title>C&#45;&gt;H</title>\n",
104+
"<path fill=\"none\" stroke=\"black\" d=\"M161.09,-90.98C168.01,-93.85 175.73,-96.79 183,-99 216.78,-109.26 256.39,-116.9 284.31,-121.58\"/>\n",
105+
"<polygon fill=\"black\" stroke=\"black\" points=\"283.96,-125.08 294.4,-123.23 285.09,-118.17 283.96,-125.08\"/>\n",
106+
"<text text-anchor=\"middle\" x=\"193.5\" y=\"-107.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">300</text>\n",
107+
"</g>\n",
108+
"<!-- D&#45;&gt;E -->\n",
109+
"<g id=\"edge7\" class=\"edge\">\n",
110+
"<title>D&#45;&gt;E</title>\n",
111+
"<path fill=\"none\" stroke=\"black\" d=\"M273.84,-31.54C293.73,-37.89 322.79,-47.3 348,-56 351.59,-57.24 355.34,-58.56 359.07,-59.89\"/>\n",
112+
"<polygon fill=\"black\" stroke=\"black\" points=\"358.17,-63.29 368.76,-63.39 360.54,-56.7 358.17,-63.29\"/>\n",
113+
"<text text-anchor=\"middle\" x=\"321\" y=\"-59.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">100</text>\n",
114+
"</g>\n",
115+
"<!-- F -->\n",
116+
"<g id=\"node7\" class=\"node\">\n",
117+
"<title>F</title>\n",
118+
"<ellipse fill=\"none\" stroke=\"black\" cx=\"393\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
119+
"<text text-anchor=\"middle\" x=\"393\" y=\"-14.3\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">F</text>\n",
120+
"</g>\n",
121+
"<!-- D&#45;&gt;F -->\n",
122+
"<g id=\"edge8\" class=\"edge\">\n",
123+
"<title>D&#45;&gt;F</title>\n",
124+
"<path fill=\"none\" stroke=\"black\" d=\"M275.53,-19.92C281.57,-19.12 288,-18.41 294,-18 314.39,-16.62 337.21,-16.53 355.7,-16.83\"/>\n",
125+
"<polygon fill=\"black\" stroke=\"black\" points=\"355.69,-20.33 365.76,-17.04 355.84,-13.33 355.69,-20.33\"/>\n",
126+
"<text text-anchor=\"middle\" x=\"321\" y=\"-21.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">250</text>\n",
127+
"</g>\n",
128+
"<!-- G -->\n",
129+
"<g id=\"node8\" class=\"node\">\n",
130+
"<title>G</title>\n",
131+
"<ellipse fill=\"none\" stroke=\"black\" cx=\"504\" cy=\"-72\" rx=\"27\" ry=\"18\"/>\n",
132+
"<text text-anchor=\"middle\" x=\"504\" y=\"-68.3\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">G</text>\n",
133+
"</g>\n",
134+
"<!-- E&#45;&gt;G -->\n",
135+
"<g id=\"edge9\" class=\"edge\">\n",
136+
"<title>E&#45;&gt;G</title>\n",
137+
"<path fill=\"none\" stroke=\"black\" d=\"M420.11,-72C434.03,-72 451.37,-72 466.6,-72\"/>\n",
138+
"<polygon fill=\"black\" stroke=\"black\" points=\"466.73,-75.5 476.73,-72 466.73,-68.5 466.73,-75.5\"/>\n",
139+
"<text text-anchor=\"middle\" x=\"448.5\" y=\"-75.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">300</text>\n",
140+
"</g>\n",
141+
"<!-- H&#45;&gt;G -->\n",
142+
"<g id=\"edge11\" class=\"edge\">\n",
143+
"<title>H&#45;&gt;G</title>\n",
144+
"<path fill=\"none\" stroke=\"black\" d=\"M346.83,-121.66C374.54,-115.43 420.53,-104.28 459,-91 463.27,-89.52 467.72,-87.82 472.06,-86.05\"/>\n",
145+
"<polygon fill=\"black\" stroke=\"black\" points=\"473.48,-89.25 481.32,-82.13 470.75,-82.8 473.48,-89.25\"/>\n",
146+
"<text text-anchor=\"middle\" x=\"393\" y=\"-119.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">400</text>\n",
147+
"</g>\n",
148+
"<!-- F&#45;&gt;G -->\n",
149+
"<g id=\"edge10\" class=\"edge\">\n",
150+
"<title>F&#45;&gt;G</title>\n",
151+
"<path fill=\"none\" stroke=\"black\" d=\"M415.28,-28.54C431.63,-36.64 454.44,-47.94 472.7,-56.99\"/>\n",
152+
"<polygon fill=\"black\" stroke=\"black\" points=\"471.26,-60.18 481.78,-61.49 474.37,-53.91 471.26,-60.18\"/>\n",
153+
"<text text-anchor=\"middle\" x=\"448.5\" y=\"-52.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">200</text>\n",
154+
"</g>\n",
155+
"</g>\n",
156+
"</svg>\n"
157+
],
158+
"text/plain": [
159+
"<graphviz.graphs.Digraph at 0x1ff94897310>"
160+
]
161+
},
162+
"metadata": {},
163+
"output_type": "display_data"
164+
}
165+
],
166+
"source": [
167+
"from graphviz import Digraph\n",
168+
"from collections import deque\n",
169+
"from IPython.display import display\n",
170+
"\n",
171+
"def distancia_maxima(grafo, inicio):\n",
172+
" repr = {nodo: (float('-inf'), []) for nodo in grafo}\n",
173+
" repr[inicio] = (0, [inicio])\n",
174+
" no_visitados = set(grafo)\n",
175+
"\n",
176+
" while no_visitados:\n",
177+
" nodo_max = max(no_visitados, key=lambda nodo: repr[nodo][0])\n",
178+
" no_visitados.remove(nodo_max)\n",
179+
"\n",
180+
" for conector, peso in grafo[nodo_max].items():\n",
181+
" if conector in no_visitados: #---------------------------------------------------\n",
182+
" nueva_distancia = repr[nodo_max][0] + peso\n",
183+
" if nueva_distancia > repr[conector][0]: #Actualizamos solo si es mayor el ancho de banda\n",
184+
" repr[conector] = (nueva_distancia, repr[nodo_max][1] + [conector])\n",
185+
" return repr\n",
186+
"\n",
187+
"def routers(grafo, inicio, fin):\n",
188+
" cola = deque([(inicio, [inicio])])\n",
189+
" visitados = set()\n",
190+
"\n",
191+
" while cola:\n",
192+
" actual, camino = cola.popleft()\n",
193+
" if actual == fin: #Fin\n",
194+
" return camino\n",
195+
"\n",
196+
" for vecino in grafo[actual]: #\n",
197+
" if vecino not in visitados:\n",
198+
" visitados.add(vecino)\n",
199+
" cola.append((vecino, camino + [vecino]))\n",
200+
"\n",
201+
"if __name__ == \"__main__\":\n",
202+
"\n",
203+
" grafo = {\n",
204+
" 'A': {'B': 200, 'C': 100},\n",
205+
" 'B': {'D': 300},\n",
206+
" 'C': {'D': 250, 'E': 200, 'H': 300},\n",
207+
" 'D': {'E': 100, 'F': 250},\n",
208+
" 'E': {'G': 300},\n",
209+
" 'F': {'G': 200},\n",
210+
" 'H': {'G': 400},\n",
211+
" 'G': {}\n",
212+
" }\n",
213+
"\n",
214+
" distancia_min = distancia_minima(grafo, 'A')\n",
215+
" distancia_max = distancia_maxima(grafo, 'A')\n",
216+
" cant_routers = routers(grafo, 'A', 'G')\n",
217+
" \n",
218+
" print(\"'A' --> 'G'\")\n",
219+
" print(f\"Mayor ancho de banda: {' > '.join(distancia_max['G'][1])} -> {distancia_max['G'][0]}mb\")\n",
220+
" print(f\"El camino más corto: {' > '.join(cant_routers)} -> {len(cant_routers) - 1} routers\")\n",
221+
" \n",
222+
" dot = Digraph(format='svg', graph_attr={'rankdir': 'LR'})\n",
223+
"\n",
224+
" for nodo, conectores in grafo.items():\n",
225+
" dot.node(nodo)\n",
226+
" for conector, peso in conectores.items():\n",
227+
" dot.edge(nodo, conector, label=str(peso))\n",
228+
"\n",
229+
"\n",
230+
" display(dot)\n"
231+
]
232+
},
233+
{
234+
"cell_type": "code",
235+
"execution_count": null,
236+
"metadata": {},
237+
"outputs": [],
238+
"source": []
239+
}
240+
],
241+
"metadata": {
242+
"kernelspec": {
243+
"display_name": "Python 3 (ipykernel)",
244+
"language": "python",
245+
"name": "python3"
246+
},
247+
"language_info": {
248+
"codemirror_mode": {
249+
"name": "ipython",
250+
"version": 3
251+
},
252+
"file_extension": ".py",
253+
"mimetype": "text/x-python",
254+
"name": "python",
255+
"nbconvert_exporter": "python",
256+
"pygments_lexer": "ipython3",
257+
"version": "3.10.14"
258+
}
259+
},
260+
"nbformat": 4,
261+
"nbformat_minor": 4
262+
}
0 Bytes
Binary file not shown.
-751 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)