Skip to content

Commit

Permalink
U08 + U09 (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
thorstendikmann committed Jul 3, 2024
1 parent c062556 commit 8285115
Show file tree
Hide file tree
Showing 10 changed files with 1,041 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/chapter/declarations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ typedef struct Array

void array_init(Array *a, int capacity);
void array_destroy(Array *a);
void array_resize(Array *a, int new_capacity);
void array_insert(Array *a, int idx, char value);
void array_push_back(Array *a, char value);
char array_remove(Array *a, int idx);
Expand Down Expand Up @@ -143,6 +144,7 @@ class BinTree
std::string traverseDFS_inorder() const;
std::string traverseBFS() const;

protected:
node *left;
node *right;
T value;
Expand Down
134 changes: 134 additions & 0 deletions src/uebungen/uebung08/graph_essentials/graph_representation.ipynb
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
}
131 changes: 131 additions & 0 deletions src/uebungen/uebung08/graph_essentials/graph_toposort.ipynb
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
}
Loading

0 comments on commit 8285115

Please sign in to comment.