Skip to content

Commit 00d1cf5

Browse files
committed
graphs 1 update
1 parent d44349e commit 00d1cf5

File tree

2 files changed

+406
-4
lines changed

2 files changed

+406
-4
lines changed

Graphs/.ipynb_checkpoints/Implementation of Adjacency List-checkpoint.ipynb

+203-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,214 @@
11
{
22
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Implementation of a Graph as an Adjacency List\n",
8+
"\n",
9+
"\n",
10+
"Using dictionaries, it is easy to implement the adjacency list in Python. In our implementation of the Graph abstract data type we will create two classes: **Graph**, which holds the master list of vertices, and **Vertex**, which will represent each vertex in the graph.\n",
11+
"\n",
12+
"Each Vertex uses a dictionary to keep track of the vertices to which it is connected, and the weight of each edge. This dictionary is called **connectedTo**. The constructor simply initializes the id, which will typically be a string, and the **connectedTo** dictionary. The **addNeighbor** method is used add a connection from this vertex to another. The **getConnections** method returns all of the vertices in the adjacency list, as represented by the **connectedTo** instance variable. The **getWeight** method returns the weight of the edge from this vertex to the vertex passed as a parameter."
13+
]
14+
},
315
{
416
"cell_type": "code",
5-
"execution_count": null,
17+
"execution_count": 3,
618
"metadata": {
719
"collapsed": true
820
},
921
"outputs": [],
10-
"source": []
22+
"source": [
23+
"class Vertex:\n",
24+
" def __init__(self,key):\n",
25+
" self.id = key\n",
26+
" self.connectedTo = {}\n",
27+
"\n",
28+
" def addNeighbor(self,nbr,weight=0):\n",
29+
" self.connectedTo[nbr] = weight\n",
30+
"\n",
31+
" def __str__(self):\n",
32+
" return str(self.id) + ' connectedTo: ' + str([x.id for x in self.connectedTo])\n",
33+
"\n",
34+
" def getConnections(self):\n",
35+
" return self.connectedTo.keys()\n",
36+
"\n",
37+
" def getId(self):\n",
38+
" return self.id\n",
39+
"\n",
40+
" def getWeight(self,nbr):\n",
41+
" return self.connectedTo[nbr]"
42+
]
43+
},
44+
{
45+
"cell_type": "markdown",
46+
"metadata": {
47+
"collapsed": true
48+
},
49+
"source": [
50+
"In order to implement a Graph as an Adjacency List what we need to do is define the methods our Adjacency List object will have:\n",
51+
"\n",
52+
"* **Graph()** creates a new, empty graph.\n",
53+
"* **addVertex(vert)** adds an instance of Vertex to the graph.\n",
54+
"* **addEdge(fromVert, toVert)** Adds a new, directed edge to the graph that connects two vertices.\n",
55+
"* **addEdge(fromVert, toVert, weight)** Adds a new, weighted, directed edge to the graph that connects two vertices.\n",
56+
"* **getVertex(vertKey)** finds the vertex in the graph named vertKey.\n",
57+
"* **getVertices()** returns the list of all vertices in the graph. \n",
58+
"* **in** returns True for a statement of the form vertex in graph, if the given vertex is in the graph, False otherwise."
59+
]
60+
},
61+
{
62+
"cell_type": "code",
63+
"execution_count": 4,
64+
"metadata": {
65+
"collapsed": true
66+
},
67+
"outputs": [],
68+
"source": [
69+
"class Graph:\n",
70+
" def __init__(self):\n",
71+
" self.vertList = {}\n",
72+
" self.numVertices = 0\n",
73+
"\n",
74+
" def addVertex(self,key):\n",
75+
" self.numVertices = self.numVertices + 1\n",
76+
" newVertex = Vertex(key)\n",
77+
" self.vertList[key] = newVertex\n",
78+
" return newVertex\n",
79+
"\n",
80+
" def getVertex(self,n):\n",
81+
" if n in self.vertList:\n",
82+
" return self.vertList[n]\n",
83+
" else:\n",
84+
" return None\n",
85+
"\n",
86+
" def __contains__(self,n):\n",
87+
" return n in self.vertList\n",
88+
"\n",
89+
" def addEdge(self,f,t,cost=0):\n",
90+
" if f not in self.vertList:\n",
91+
" nv = self.addVertex(f)\n",
92+
" if t not in self.vertList:\n",
93+
" nv = self.addVertex(t)\n",
94+
" self.vertList[f].addNeighbor(self.vertList[t], cost)\n",
95+
"\n",
96+
" def getVertices(self):\n",
97+
" return self.vertList.keys()\n",
98+
"\n",
99+
" def __iter__(self):\n",
100+
" return iter(self.vertList.values())"
101+
]
102+
},
103+
{
104+
"cell_type": "markdown",
105+
"metadata": {},
106+
"source": [
107+
"Let's see a simple example of how to use this:"
108+
]
109+
},
110+
{
111+
"cell_type": "code",
112+
"execution_count": 5,
113+
"metadata": {
114+
"collapsed": false
115+
},
116+
"outputs": [],
117+
"source": [
118+
"g = Graph()\n",
119+
"for i in range(6):\n",
120+
" g.addVertex(i)"
121+
]
122+
},
123+
{
124+
"cell_type": "code",
125+
"execution_count": 6,
126+
"metadata": {
127+
"collapsed": false
128+
},
129+
"outputs": [
130+
{
131+
"data": {
132+
"text/plain": [
133+
"{0: <__main__.Vertex instance at 0x10476b680>,\n",
134+
" 1: <__main__.Vertex instance at 0x104cce5f0>,\n",
135+
" 2: <__main__.Vertex instance at 0x10395d950>,\n",
136+
" 3: <__main__.Vertex instance at 0x1039c00e0>,\n",
137+
" 4: <__main__.Vertex instance at 0x1039c4e60>,\n",
138+
" 5: <__main__.Vertex instance at 0x1039c45f0>}"
139+
]
140+
},
141+
"execution_count": 6,
142+
"metadata": {},
143+
"output_type": "execute_result"
144+
}
145+
],
146+
"source": [
147+
"g.vertList"
148+
]
149+
},
150+
{
151+
"cell_type": "code",
152+
"execution_count": 7,
153+
"metadata": {
154+
"collapsed": true
155+
},
156+
"outputs": [],
157+
"source": [
158+
"g.addEdge(0,1,2)"
159+
]
160+
},
161+
{
162+
"cell_type": "code",
163+
"execution_count": 12,
164+
"metadata": {
165+
"collapsed": false
166+
},
167+
"outputs": [
168+
{
169+
"name": "stdout",
170+
"output_type": "stream",
171+
"text": [
172+
"0 connectedTo: [1]\n",
173+
"[<__main__.Vertex instance at 0x104cce5f0>]\n",
174+
"\n",
175+
"\n",
176+
"1 connectedTo: []\n",
177+
"[]\n",
178+
"\n",
179+
"\n",
180+
"2 connectedTo: []\n",
181+
"[]\n",
182+
"\n",
183+
"\n",
184+
"3 connectedTo: []\n",
185+
"[]\n",
186+
"\n",
187+
"\n",
188+
"4 connectedTo: []\n",
189+
"[]\n",
190+
"\n",
191+
"\n",
192+
"5 connectedTo: []\n",
193+
"[]\n",
194+
"\n",
195+
"\n"
196+
]
197+
}
198+
],
199+
"source": [
200+
"for vertex in g:\n",
201+
" print vertex\n",
202+
" print vertex.getConnections()\n",
203+
" print '\\n'"
204+
]
205+
},
206+
{
207+
"cell_type": "markdown",
208+
"metadata": {},
209+
"source": [
210+
"# Great Job!"
211+
]
11212
}
12213
],
13214
"metadata": {

0 commit comments

Comments
 (0)