initial upload of all course files
This commit is contained in:
175
Section_5/Graph Implementation Using Adjacency Matrix.ipynb
Normal file
175
Section_5/Graph Implementation Using Adjacency Matrix.ipynb
Normal file
@@ -0,0 +1,175 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Graph Implementation Using Adjacency Matrix\n",
|
||||
"for undirected graph, with weighted or unweighted edges. \n",
|
||||
"© Joe James, 2019."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Vertex Class\n",
|
||||
"A vertex object only needs to store its name. "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Vertex:\n",
|
||||
" def __init__(self, n):\n",
|
||||
" self.name = n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Graph Class\n",
|
||||
"A graph object has three attributes: \n",
|
||||
"**vertices** - a dictionary with vertex_name:vertex_object. \n",
|
||||
"**edges** - a 2-dimensional list (ie. a matrix) of edges. for an unweighted graph it will contain 0 for no edge and 1 for edge. \n",
|
||||
"**edge_indices** - a dictionary with vertex_name:list_index (eg. A:0) to access edges. \n",
|
||||
"add_vertex updates all three of these attributes. \n",
|
||||
"add_edge only needs to update the edges matrix."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Graph:\n",
|
||||
" vertices = {}\n",
|
||||
" edges = []\n",
|
||||
" edge_indices = {}\n",
|
||||
" \n",
|
||||
" def add_vertex(self, vertex):\n",
|
||||
" if isinstance(vertex, Vertex) and vertex.name not in self.vertices:\n",
|
||||
" self.vertices[vertex.name] = vertex\n",
|
||||
" # for loop appends a column of zeros to the edges matrix\n",
|
||||
" for row in self.edges:\n",
|
||||
" row.append(0)\n",
|
||||
" # append a row of zeros to the bottom of the edges matrix\n",
|
||||
" self.edges.append([0] * (len(self.edges)+1))\n",
|
||||
" self.edge_indices[vertex.name] = len(self.edge_indices)\n",
|
||||
" return True\n",
|
||||
" else:\n",
|
||||
" return False\n",
|
||||
" \n",
|
||||
" def add_edge(self, u, v, weight=1):\n",
|
||||
" if u in self.vertices and v in self.vertices:\n",
|
||||
" self.edges[self.edge_indices[u]][self.edge_indices[v]] = weight\n",
|
||||
" self.edges[self.edge_indices[v]][self.edge_indices[u]] = weight\n",
|
||||
" return True\n",
|
||||
" else:\n",
|
||||
" return False\n",
|
||||
" \n",
|
||||
" def print_graph(self):\n",
|
||||
" for v, i in sorted(self.edge_indices.items()):\n",
|
||||
" print(v + ' ', end='')\n",
|
||||
" for j in range(len(self.edges)):\n",
|
||||
" print(self.edges[i][j], end=' ')\n",
|
||||
" print(' ')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Test Code\n",
|
||||
"Here we create a new Graph object. We create a new vertex named A. We add A to the graph. Then we add new vertex B to the graph. Then we iterate from A to K and add a bunch of vertices to the graph. Since the add_vertex method checks for duplicates, A and B are not added twice.\n",
|
||||
"This is exactly the same test code we used for the graph with adjacency lists."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"g = Graph()\n",
|
||||
"a = Vertex('A')\n",
|
||||
"g.add_vertex(a)\n",
|
||||
"g.add_vertex(Vertex('B'))\n",
|
||||
"for i in range(ord('A'), ord('K')):\n",
|
||||
" g.add_vertex(Vertex(chr(i)))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"An edge consists of two vertex names. Here we iterate through a list of edges and add each to the graph. \n",
|
||||
"\n",
|
||||
"This print_graph method doesn't give a very good visualization of the graph, but it does show the adjacency matrix so we can see each vertex's neighbors."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"A 0 1 0 0 1 0 0 0 0 0 \n",
|
||||
"B 1 0 0 0 0 1 0 0 0 0 \n",
|
||||
"C 0 0 0 0 0 0 1 0 0 0 \n",
|
||||
"D 0 0 0 0 1 0 0 1 0 0 \n",
|
||||
"E 1 0 0 1 0 0 0 1 0 0 \n",
|
||||
"F 0 1 0 0 0 0 1 0 1 1 \n",
|
||||
"G 0 0 1 0 0 1 0 0 0 1 \n",
|
||||
"H 0 0 0 1 1 0 0 0 1 0 \n",
|
||||
"I 0 0 0 0 0 1 0 1 0 0 \n",
|
||||
"J 0 0 0 0 0 1 1 0 0 0 \n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"edges = ['AB', 'AE', 'BF', 'CG', 'DE', 'DH', 'EH', 'FG', 'FI', 'FJ', 'GJ', 'HI']\n",
|
||||
"for edge in edges:\n",
|
||||
" g.add_edge(edge[0], edge[1])\n",
|
||||
"\n",
|
||||
"g.print_graph()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"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.7.0"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
Reference in New Issue
Block a user