Skip to content

Commit 86e1f15

Browse files
committed
word ladder
1 parent 00d1cf5 commit 86e1f15

File tree

3 files changed

+244
-2
lines changed

3 files changed

+244
-2
lines changed

Graphs/.ipynb_checkpoints/Word Ladder Example Problem-checkpoint.ipynb

Lines changed: 119 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,131 @@
11
{
22
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {
6+
"collapsed": true
7+
},
8+
"source": [
9+
"# Word Ladder Example Code\n",
10+
"\n",
11+
"Below is the Vertex and Graph class used for the Word Ladder example code:"
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": 1,
17+
"metadata": {
18+
"collapsed": true
19+
},
20+
"outputs": [],
21+
"source": [
22+
"class Vertex:\n",
23+
" def __init__(self,key):\n",
24+
" self.id = key\n",
25+
" self.connectedTo = {}\n",
26+
"\n",
27+
" def addNeighbor(self,nbr,weight=0):\n",
28+
" self.connectedTo[nbr] = weight\n",
29+
"\n",
30+
" def __str__(self):\n",
31+
" return str(self.id) + ' connectedTo: ' + str([x.id for x in self.connectedTo])\n",
32+
"\n",
33+
" def getConnections(self):\n",
34+
" return self.connectedTo.keys()\n",
35+
"\n",
36+
" def getId(self):\n",
37+
" return self.id\n",
38+
"\n",
39+
" def getWeight(self,nbr):\n",
40+
" return self.connectedTo[nbr]"
41+
]
42+
},
343
{
444
"cell_type": "code",
5-
"execution_count": null,
45+
"execution_count": 2,
646
"metadata": {
747
"collapsed": true
848
},
949
"outputs": [],
50+
"source": [
51+
"class Graph:\n",
52+
" def __init__(self):\n",
53+
" self.vertList = {}\n",
54+
" self.numVertices = 0\n",
55+
"\n",
56+
" def addVertex(self,key):\n",
57+
" self.numVertices = self.numVertices + 1\n",
58+
" newVertex = Vertex(key)\n",
59+
" self.vertList[key] = newVertex\n",
60+
" return newVertex\n",
61+
"\n",
62+
" def getVertex(self,n):\n",
63+
" if n in self.vertList:\n",
64+
" return self.vertList[n]\n",
65+
" else:\n",
66+
" return None\n",
67+
"\n",
68+
" def __contains__(self,n):\n",
69+
" return n in self.vertList\n",
70+
"\n",
71+
" def addEdge(self,f,t,cost=0):\n",
72+
" if f not in self.vertList:\n",
73+
" nv = self.addVertex(f)\n",
74+
" if t not in self.vertList:\n",
75+
" nv = self.addVertex(t)\n",
76+
" self.vertList[f].addNeighbor(self.vertList[t], cost)\n",
77+
"\n",
78+
" def getVertices(self):\n",
79+
" return self.vertList.keys()\n",
80+
"\n",
81+
" def __iter__(self):\n",
82+
" return iter(self.vertList.values())"
83+
]
84+
},
85+
{
86+
"cell_type": "markdown",
87+
"metadata": {},
1088
"source": []
89+
},
90+
{
91+
"cell_type": "code",
92+
"execution_count": 19,
93+
"metadata": {
94+
"collapsed": true
95+
},
96+
"outputs": [],
97+
"source": [
98+
"def buildGraph(wordFile):\n",
99+
" d = {}\n",
100+
" g = Graph()\n",
101+
" \n",
102+
" wfile = open(wordFile,'r')\n",
103+
" # create buckets of words that differ by one letter\n",
104+
" for line in wfile:\n",
105+
" print line\n",
106+
" word = line[:-1]\n",
107+
" print word\n",
108+
" for i in range(len(word)):\n",
109+
" bucket = word[:i] + '_' + word[i+1:]\n",
110+
" if bucket in d:\n",
111+
" d[bucket].append(word)\n",
112+
" else:\n",
113+
" d[bucket] = [word]\n",
114+
" # add vertices and edges for words in the same bucket\n",
115+
" for bucket in d.keys():\n",
116+
" for word1 in d[bucket]:\n",
117+
" for word2 in d[bucket]:\n",
118+
" if word1 != word2:\n",
119+
" g.addEdge(word1,word2)\n",
120+
" return g"
121+
]
122+
},
123+
{
124+
"cell_type": "markdown",
125+
"metadata": {},
126+
"source": [
127+
"Please reference the video for full explanation!"
128+
]
11129
}
12130
],
13131
"metadata": {

Graphs/Word Ladder Example Problem.ipynb

Lines changed: 119 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,131 @@
11
{
22
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {
6+
"collapsed": true
7+
},
8+
"source": [
9+
"# Word Ladder Example Code\n",
10+
"\n",
11+
"Below is the Vertex and Graph class used for the Word Ladder example code:"
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": 1,
17+
"metadata": {
18+
"collapsed": true
19+
},
20+
"outputs": [],
21+
"source": [
22+
"class Vertex:\n",
23+
" def __init__(self,key):\n",
24+
" self.id = key\n",
25+
" self.connectedTo = {}\n",
26+
"\n",
27+
" def addNeighbor(self,nbr,weight=0):\n",
28+
" self.connectedTo[nbr] = weight\n",
29+
"\n",
30+
" def __str__(self):\n",
31+
" return str(self.id) + ' connectedTo: ' + str([x.id for x in self.connectedTo])\n",
32+
"\n",
33+
" def getConnections(self):\n",
34+
" return self.connectedTo.keys()\n",
35+
"\n",
36+
" def getId(self):\n",
37+
" return self.id\n",
38+
"\n",
39+
" def getWeight(self,nbr):\n",
40+
" return self.connectedTo[nbr]"
41+
]
42+
},
343
{
444
"cell_type": "code",
5-
"execution_count": null,
45+
"execution_count": 2,
646
"metadata": {
747
"collapsed": true
848
},
949
"outputs": [],
50+
"source": [
51+
"class Graph:\n",
52+
" def __init__(self):\n",
53+
" self.vertList = {}\n",
54+
" self.numVertices = 0\n",
55+
"\n",
56+
" def addVertex(self,key):\n",
57+
" self.numVertices = self.numVertices + 1\n",
58+
" newVertex = Vertex(key)\n",
59+
" self.vertList[key] = newVertex\n",
60+
" return newVertex\n",
61+
"\n",
62+
" def getVertex(self,n):\n",
63+
" if n in self.vertList:\n",
64+
" return self.vertList[n]\n",
65+
" else:\n",
66+
" return None\n",
67+
"\n",
68+
" def __contains__(self,n):\n",
69+
" return n in self.vertList\n",
70+
"\n",
71+
" def addEdge(self,f,t,cost=0):\n",
72+
" if f not in self.vertList:\n",
73+
" nv = self.addVertex(f)\n",
74+
" if t not in self.vertList:\n",
75+
" nv = self.addVertex(t)\n",
76+
" self.vertList[f].addNeighbor(self.vertList[t], cost)\n",
77+
"\n",
78+
" def getVertices(self):\n",
79+
" return self.vertList.keys()\n",
80+
"\n",
81+
" def __iter__(self):\n",
82+
" return iter(self.vertList.values())"
83+
]
84+
},
85+
{
86+
"cell_type": "markdown",
87+
"metadata": {},
1088
"source": []
89+
},
90+
{
91+
"cell_type": "code",
92+
"execution_count": 19,
93+
"metadata": {
94+
"collapsed": true
95+
},
96+
"outputs": [],
97+
"source": [
98+
"def buildGraph(wordFile):\n",
99+
" d = {}\n",
100+
" g = Graph()\n",
101+
" \n",
102+
" wfile = open(wordFile,'r')\n",
103+
" # create buckets of words that differ by one letter\n",
104+
" for line in wfile:\n",
105+
" print line\n",
106+
" word = line[:-1]\n",
107+
" print word\n",
108+
" for i in range(len(word)):\n",
109+
" bucket = word[:i] + '_' + word[i+1:]\n",
110+
" if bucket in d:\n",
111+
" d[bucket].append(word)\n",
112+
" else:\n",
113+
" d[bucket] = [word]\n",
114+
" # add vertices and edges for words in the same bucket\n",
115+
" for bucket in d.keys():\n",
116+
" for word1 in d[bucket]:\n",
117+
" for word2 in d[bucket]:\n",
118+
" if word1 != word2:\n",
119+
" g.addEdge(word1,word2)\n",
120+
" return g"
121+
]
122+
},
123+
{
124+
"cell_type": "markdown",
125+
"metadata": {},
126+
"source": [
127+
"Please reference the video for full explanation!"
128+
]
11129
}
12130
],
13131
"metadata": {

Graphs/words.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pope
2+
rope
3+
sage
4+
best
5+
ripe
6+
pipe

0 commit comments

Comments
 (0)