-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c062556
commit 8285115
Showing
10 changed files
with
1,041 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,134 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "5bf06d6e-9ce1-4d2f-8fe3-528206558f45", | ||
"metadata": {}, | ||
"source": [ | ||
"# Graph representation" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "168dbc31", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from importlib import reload\n", | ||
"\n", | ||
"import logging\n", | ||
"logging.basicConfig(level=logging.INFO,format='%(levelname)s - %(message)s')\n", | ||
"logging.getLogger(\"graph.undirectedgraph\").setLevel(logging.INFO)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "6c67ee38", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Add the data structure folder to path\n", | ||
"import sys\n", | ||
"sys.path.insert(0,'../../../chapter/05_graph/')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "c203606e", | ||
"metadata": {}, | ||
"source": [ | ||
"Geben Sie zu folgendem Graph eine Repräsentation an in Form einer Adjazenzmatrix / Adjazenzliste an." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "aae75ce6-ddeb-4b71-8c34-abd6746277f5", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"%matplotlib inline\n", | ||
"\n", | ||
"# Prevent caching of modules\n", | ||
"from importlib import reload\n", | ||
"\n", | ||
"import networkx as nx\n", | ||
"\n", | ||
"from graph.undirectedgraph import * \n", | ||
"import matplotlib.pyplot as plt\n", | ||
"\n", | ||
"g = Graph()\n", | ||
"g.add_node(Node(\"A\"))\n", | ||
"g.add_node(Node(\"B\"))\n", | ||
"g.add_node(Node(\"C\"))\n", | ||
"g.add_node(Node(\"D\"))\n", | ||
"g.add_node(Node(\"E\"))\n", | ||
"g.add_node(Node(\"F\"))\n", | ||
"g.add_node(Node(\"G\"))\n", | ||
"g.add_node(Node(\"H\"))\n", | ||
"\n", | ||
"g.add_edge(0,1)\n", | ||
"g.add_edge(0,4)\n", | ||
"g.add_edge(1,2)\n", | ||
"g.add_edge(1,3)\n", | ||
"g.add_edge(1,7)\n", | ||
"g.add_edge(2,5)\n", | ||
"g.add_edge(2,6)\n", | ||
"g.add_edge(2,7)\n", | ||
"g.add_edge(3,5)\n", | ||
"g.add_edge(3,7)\n", | ||
"g.add_edge(4,7)\n", | ||
"\n", | ||
"nx.draw(g.toNetworkx(), with_labels=True, font_weight=\"bold\", font_color=\"#FFFFFF\", node_size=500, alpha=0.95)\n", | ||
"plt.show()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "1b41a008", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Adjacency matrix\n", | ||
"g.print_matrix()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "9e84216d", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Adjacency list -- different representations\n", | ||
"print(f\"Graph Adjacency List (label): {g.getLabelGraphAdjacencyList()}\")\n", | ||
"print(f\"Graph Adjacency List (index): {g.getIndexGraphAdjacencyList()}\")\n", | ||
"print(f\"Graph Adjacency List (nodes): {g.getNodeGraphAdjacencyList()}\")" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.10.12" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
This file contains 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,131 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "5bf06d6e-9ce1-4d2f-8fe3-528206558f45", | ||
"metadata": {}, | ||
"source": [ | ||
"# Graph - Topological Sorting" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "168dbc31", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from importlib import reload\n", | ||
"\n", | ||
"import logging\n", | ||
"logging.basicConfig(level=logging.INFO,format='%(levelname)s - %(message)s')\n", | ||
"logging.getLogger(\"graph.undirectedgraph\").setLevel(logging.DEBUG)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "6c67ee38", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Add the data structure folder to path\n", | ||
"import sys\n", | ||
"sys.path.insert(0,'../../../chapter/05_graph/')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "2d2d97ff", | ||
"metadata": {}, | ||
"source": [ | ||
"Bestimmen Sie eine Topologische Sortierung für den folgenden gerichteten Graphen. Geben Sie die hierbei die Anzahl der Vorgänger sowie alle bereits topologisch sortieren Knoten in jedem Schritt tabellarisch an." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "aae75ce6-ddeb-4b71-8c34-abd6746277f5", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"%matplotlib inline\n", | ||
"\n", | ||
"# Prevent caching of modules\n", | ||
"from importlib import reload\n", | ||
"\n", | ||
"import networkx as nx\n", | ||
"\n", | ||
"from graph.directedgraph import * \n", | ||
"import matplotlib.pyplot as plt\n", | ||
"\n", | ||
"g = DirectedGraph()\n", | ||
"g.add_node(Node(\"A\"))\n", | ||
"g.add_node(Node(\"B\"))\n", | ||
"g.add_node(Node(\"C\"))\n", | ||
"g.add_node(Node(\"D\"))\n", | ||
"g.add_node(Node(\"E\"))\n", | ||
"g.add_node(Node(\"F\"))\n", | ||
"g.add_node(Node(\"G\"))\n", | ||
"g.add_node(Node(\"H\"))\n", | ||
"\n", | ||
"g.add_edge(0,1)\n", | ||
"g.add_edge(0,4)\n", | ||
"g.add_edge(1,2)\n", | ||
"g.add_edge(1,3)\n", | ||
"g.add_edge(1,7)\n", | ||
"g.add_edge(2,5)\n", | ||
"g.add_edge(2,6)\n", | ||
"g.add_edge(2,7)\n", | ||
"g.add_edge(3,5)\n", | ||
"g.add_edge(3,7)\n", | ||
"g.add_edge(4,7)\n", | ||
"g.add_edge(5,6)\n", | ||
"\n", | ||
"nx.draw(g.toNetworkx(), with_labels=True, font_weight=\"bold\", font_color=\"#FFFFFF\", node_size=500, alpha=0.95)\n", | ||
"plt.show()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "85431b2c", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"g.print_matrix()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "1b41a008", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"print(f\"topological_sort_kahn -> {g.topological_sort_Kahn()}\")" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.10.12" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
Oops, something went wrong.