summaryrefslogtreecommitdiff
path: root/Data_Structures_and_Algorithms_in_Java/ch13.ipynb
blob: ab8fea532d075b53ed43a0cfa021c043ccb24165 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
{
 "metadata": {
  "name": "",
  "signature": "sha256:e4ee94b1921cc19d6665af422d1e40ebb2336538caa9a0070a2828a133297159"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Chapter 13 : Graphs"
     ]
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 13.1  Page no: 631"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      " \n",
      "class StackX:\n",
      "    def __init__(self):\n",
      "        self.st = [] # make array\n",
      "        self.top = -1\n",
      "\n",
      "    def push(self,j): # put item on stack\n",
      "        self.top += 1\n",
      "        self.st.append(j)\n",
      "\n",
      "    def pop(self): # take item off stack\n",
      "        self.top -= 1\n",
      "        return self.st.pop()\n",
      "\n",
      "    def peek(self): # peek at top of stack\n",
      "        return self.st[self.top]\n",
      "    \n",
      "    def isEmpty(self): # true if nothing on stack-\n",
      "        return self.top == -1\n",
      "\n",
      "class Vertex:\n",
      "    def __init__(self,lab): # constructor\n",
      "        self.label = lab\n",
      "        self.wasVisited = False\n",
      "\n",
      "class Graph:\n",
      "    def __init__(self):\n",
      "        self.vertexList = [] # adjacency matrix\n",
      "        self.adjMat = []\n",
      "        self.nVerts = 0\n",
      "        for j in range(20): # set adjacency\n",
      "            l = []\n",
      "            for k in range(20):\n",
      "                l.append(0)\n",
      "            self.adjMat.append(l)\n",
      "        self.theStack = StackX()\n",
      "\n",
      "    def addVertex(self,lab):\n",
      "        self.vertexList.append( Vertex(lab))\n",
      "        self.nVerts += 1\n",
      "\n",
      "    def addEdge(self,start, end):\n",
      "        self.adjMat[start][end] = 1\n",
      "        self.adjMat[end][start] = 1\n",
      "\n",
      "    def displayVertex(self,v):\n",
      "        print self.vertexList[v].label ,\n",
      "\n",
      "    def dfs(self): # depth-first search # begin at vertex 0\n",
      "        self.vertexList[0].wasVisited = True # mark it\n",
      "        self.displayVertex(0)     # display it\n",
      "        self.theStack.push(0) # push it\n",
      "        while( not self.theStack.isEmpty() ): # until stack empty,\n",
      "            # get an unvisited vertex adjacent to stack top\n",
      "            v = self.getAdjUnvisitedVertex( self.theStack.peek() )\n",
      "            if(v == -1): # if no such vertex,\n",
      "                self.theStack.pop()\n",
      "            else: # if it exists,\n",
      "                self.vertexList[v].wasVisited = True # mark it\n",
      "                self.displayVertex(v) # display it\n",
      "                self.theStack.push(v) # push it\n",
      "\n",
      "        # stack is empty, so we're done\n",
      "        for j in range(self.nVerts): # reset flags\n",
      "            self.vertexList[j].wasVisited = False  # end dfs\n",
      "\n",
      "    def getAdjUnvisitedVertex(self,v):\n",
      "        for j in range(self.nVerts):\n",
      "            if(self.adjMat[v][j]==1 and self.vertexList[j].wasVisited==False):\n",
      "                return j\n",
      "        return -1\n",
      "\n",
      "theGraph = Graph()\n",
      "theGraph.addVertex('A') # 0 (start for dfs)\n",
      "theGraph.addVertex('B') # 1\n",
      "theGraph.addVertex('C') # 2\n",
      "theGraph.addVertex('D') # 3\n",
      "theGraph.addVertex('E') # 4\n",
      "theGraph.addEdge(0,1)\n",
      "theGraph.addEdge(1,2)\n",
      "theGraph.addEdge(0,3)\n",
      "theGraph.addEdge(3,4)\n",
      "print 'Visits: ',\n",
      "theGraph.dfs() # depth-first search"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Visits:  A B C D E\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 13.2  Page no : 639"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      " \n",
      "class Queue:\n",
      "    def __init__(self): # constructor\n",
      "        self.SIZE = 20\n",
      "        self.queArray = []\n",
      "        for i in range(20):\n",
      "            self.queArray.append(0)\n",
      "        self.front = 0\n",
      "        self.rear = -1\n",
      "\n",
      "    def insert(self,j): # put item at self.rear of queue\n",
      "        if(self.rear == self.SIZE-1):\n",
      "            self.rear = -1\n",
      "        self.rear += 1\n",
      "        self.queArray[self.rear] = j\n",
      "\n",
      "    def remove(self): # take item from front of queue\n",
      "        temp = self.queArray[self.front]\n",
      "        self.front += 1\n",
      "        if(self.front == self.SIZE):\n",
      "            self.front = 0\n",
      "        return temp\n",
      "\n",
      "    def isEmpty(self): # true if queue is empty\n",
      "        return ( self.rear+1==self.front or (self.front+self.SIZE-1==self.rear) )\n",
      "\n",
      "class Vertex:\n",
      "    def __init__(self,lab): # constructor\n",
      "        self.label = lab\n",
      "        self.wasVisited = False\n",
      "\n",
      "class Graph:\n",
      "    def __init__(self):\n",
      "        self.vertexList = [] # adjacency matrix\n",
      "        self.adjMat = []\n",
      "        self.nVerts = 0\n",
      "        for j in range(20): # set adjacency\n",
      "            l = []\n",
      "            for k in range(20):\n",
      "                l.append(0)\n",
      "            self.adjMat.append(l)\n",
      "        self.theQueue = Queue()\n",
      "\n",
      "    def addVertex(self,lab):\n",
      "        self.vertexList.append( Vertex(lab))\n",
      "        self.nVerts += 1\n",
      "\n",
      "    def addEdge(self,start, end):\n",
      "        self.adjMat[start][end] = 1\n",
      "        self.adjMat[end][start] = 1\n",
      "\n",
      "    def displayVertex(self,v):\n",
      "        print self.vertexList[v].label ,\n",
      "\n",
      "    def bfs(self): # breadth-first search\n",
      "        # begin at vertex 0\n",
      "        self.vertexList[0].wasVisited = True # mark it\n",
      "        self.displayVertex(0) # display it\n",
      "        self.theQueue.insert(0)  # insert at tail\n",
      "        while( not self.theQueue.isEmpty() ): # until queue empty,\n",
      "            v1 = self.theQueue.remove() # remove vertex at head\n",
      "            # until it has no unvisited neighbors\n",
      "            while( self.getAdjUnvisitedVertex(v1) != -1 ):\n",
      "                v2 = self.getAdjUnvisitedVertex(v1)\n",
      "                self.vertexList[v2].wasVisited = True # mark it\n",
      "                self.displayVertex(v2) # display it\n",
      "                self.theQueue.insert(v2) # insert it\n",
      "        for j in range(self.nVerts): # reset flags\n",
      "            self.vertexList[j].wasVisited = False\n",
      "\n",
      "    def getAdjUnvisitedVertex(self,v):\n",
      "        for j in range(self.nVerts):\n",
      "            if(self.adjMat[v][j]==1 and self.vertexList[j].wasVisited==False):\n",
      "                return j\n",
      "        return -1\n",
      "\n",
      "\n",
      "theGraph = Graph()\n",
      "theGraph.addVertex('A') # 0 (start for dfs)\n",
      "theGraph.addVertex('B') # 1\n",
      "theGraph.addVertex('C') # 2\n",
      "theGraph.addVertex('D') # 3\n",
      "theGraph.addVertex('E') # 4\n",
      "theGraph.addEdge(0,1)\n",
      "theGraph.addEdge(1,2)\n",
      "theGraph.addEdge(0,3)\n",
      "theGraph.addEdge(3,4)\n",
      "print 'Visits: ',\n",
      "theGraph.bfs() # breadth-first search"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Visits:  A B D C E\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 13.3 Page no : 645"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      " \n",
      "class StackX:\n",
      "    def __init__(self):\n",
      "        self.st = [] # make array\n",
      "        self.top = -1\n",
      "\n",
      "    def push(self,j): # put item on stack\n",
      "        self.top += 1\n",
      "        self.st.append(j)\n",
      "\n",
      "    def pop(self): # take item off stack\n",
      "        self.top -= 1\n",
      "        return self.st.pop()\n",
      "\n",
      "    def peek(self): # peek at top of stack\n",
      "        return self.st[self.top]\n",
      "    \n",
      "    def isEmpty(self): # true if nothing on stack-\n",
      "        return self.top == -1\n",
      "\n",
      "class Vertex:\n",
      "    def __init__(self,lab): # constructor\n",
      "        self.label = lab\n",
      "        self.wasVisited = False\n",
      "\n",
      "class Graph:\n",
      "    def __init__(self):\n",
      "        self.vertexList = [] # adjacency matrix\n",
      "        self.adjMat = []\n",
      "        self.nVerts = 0\n",
      "        for j in range(20): # set adjacency\n",
      "            l = []\n",
      "            for k in range(20):\n",
      "                l.append(0)\n",
      "            self.adjMat.append(l)\n",
      "        self.theStack = StackX()\n",
      "\n",
      "    def addVertex(self,lab):\n",
      "        self.vertexList.append( Vertex(lab))\n",
      "        self.nVerts += 1\n",
      "\n",
      "    def addEdge(self,start, end):\n",
      "        self.adjMat[start][end] = 1\n",
      "        self.adjMat[end][start] = 1\n",
      "\n",
      "    def displayVertex(self,v):\n",
      "        print self.vertexList[v].label ,\n",
      "\n",
      "    def mst(self): # minimum spanning tree (depth first)\n",
      "        # start at 0\n",
      "        self.vertexList[0].wasVisited =True\n",
      "        # mark it\n",
      "        self.theStack.push(0)\n",
      "        # push it\n",
      "        while(not self.theStack.isEmpty() ):\n",
      "            # until stack empty\n",
      "            # get stack top\n",
      "            currentVertex = self.theStack.peek()\n",
      "            # get next unvisited neighbor       \n",
      "            v = self.getAdjUnvisitedVertex(currentVertex)\n",
      "            if(v == -1):\n",
      "                # if no more neighbors\n",
      "                self.theStack.pop()\n",
      "            else:\n",
      "                # got a neighbor\n",
      "                self.vertexList[v].wasVisited = True # mark it\n",
      "                self.theStack.push(v)\n",
      "                # push it\n",
      "                # display edge\n",
      "                self.displayVertex(currentVertex)\n",
      "                # from currentV\n",
      "                self.displayVertex(v)\n",
      "                # to v\n",
      "                print ' ',\n",
      "        for j in range(self.nVerts):\n",
      "            # reset flags\n",
      "            self.vertexList[j].wasVisited = False\n",
      "\n",
      "    def getAdjUnvisitedVertex(self, v):\n",
      "        for j in range(self.nVerts):\n",
      "            if(self.adjMat[v][j]==1 and self.vertexList[j].wasVisited==False):\n",
      "                return j\n",
      "        return -1\n",
      "\n",
      "theGraph = Graph()\n",
      "theGraph.addVertex('A') # 0 (start for mst)\n",
      "theGraph.addVertex('B') # 1\n",
      "theGraph.addVertex('C') # 2\n",
      "theGraph.addVertex('D') # 3Topological Sorting with Directed Graphs\n",
      "theGraph.addVertex('E') # 4\n",
      "theGraph.addEdge(0,1) \n",
      "theGraph.addEdge(0,2)\n",
      "theGraph.addEdge(0,3) \n",
      "theGraph.addEdge(0,4) \n",
      "theGraph.addEdge(1,2) \n",
      "theGraph.addEdge(1,3)\n",
      "theGraph.addEdge(1,4) \n",
      "theGraph.addEdge(2,3) \n",
      "theGraph.addEdge(2,4) \n",
      "theGraph.addEdge(3,4) \n",
      "print 'Minimum spanning tree: ',\n",
      "theGraph.mst() # minimum spanning tree"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Minimum spanning tree:  A B   B C   C D   D E  \n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 13.4  page No : 657"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      " \n",
      "class Vertex:\n",
      "    def __init__(self,lab): # constructor\n",
      "        self.label = lab\n",
      "        self.wasVisited = False\n",
      "\n",
      "class Graph:\n",
      "    def __init__(self):\n",
      "        self.vertexList = [] # adjacency matrix\n",
      "        self.adjMat = []\n",
      "        self.nVerts = 0\n",
      "        for j in range(20): # set adjacency\n",
      "            l = []\n",
      "            for k in range(20):\n",
      "                l.append(0)\n",
      "            self.adjMat.append(l)\n",
      "\n",
      "    def addVertex(self,lab):\n",
      "        self.vertexList.append( Vertex(lab))\n",
      "        self.nVerts += 1\n",
      "\n",
      "    def addEdge(self,start, end):\n",
      "        self.adjMat[start][end] = 1\n",
      "        self.adjMat[end][start] = 1\n",
      "\n",
      "    def displayVertex(self,v):\n",
      "        print self.vertexList[v].label ,\n",
      "\n",
      "    def topo(self): # topological sort\n",
      "        orig_nVerts = self.nVerts # remember how many verts\n",
      "        while(self.nVerts > 0): # while vertices remain,\n",
      "            # get a vertex with no successors, or -1\n",
      "            currentVertex = self.noSuccessors()\n",
      "            if(currentVertex == -1):\n",
      "                # must be a cycleTopological Sorting with Directed Graphs\n",
      "                print 'ERROR: Graph has cycles'\n",
      "                return\n",
      "\n",
      "            # insert vertex label in sorted array (start at end)\n",
      "            self.sortedArray[nVerts-1] = self.vertexList[currentVertex].label\n",
      "            self.deleteVertex(currentVertex)\n",
      "        print 'Topologically sorted order: '\n",
      "        for j in range(orig_nVerts):\n",
      "            print sortedArray[j] ,\n",
      "        print ''\n",
      "\n",
      "    def noSuccessors(self): # returns vert with no successors\n",
      "        # (or -1 if no such verts)\n",
      "        isEdge = None\n",
      "        for row in range(self.nVerts): # for each vertex,\n",
      "            isEdge = False\n",
      "            # check edges\n",
      "            for col in range(self.nVerts):\n",
      "                if( self.adjMat[row][col] > 0 ): # if edge to\n",
      "                    # another,\n",
      "                    isEdge = True\n",
      "                    break\n",
      "            if( not isEdge ):\n",
      "                # if no edges,\n",
      "                return row\n",
      "        return -1\n",
      "\n",
      "    def deleteVertex(self,delVert):\n",
      "        if(delVert != self.nVerts-1):\n",
      "            # if not last vertex,\n",
      "            # delete from vertexList\n",
      "            for j in range(self.nVerts-1):\n",
      "                self.vertexList[j] = self.vertexList[j+1]\n",
      "            # delete row from adjMat\n",
      "        for row in range(self.nVerts-1):\n",
      "            self.moveRowUp(row, nVerts)\n",
      "            # delete col from adjMat\n",
      "        for col in range(self.nVerts-1):\n",
      "            self.moveColLeft(col, nVerts-1)\n",
      "        self.nVerts -= 1\n",
      "\n",
      "    def moveRowUp(self,row,length):\n",
      "        for col in range(length):\n",
      "            self.adjMat[row][col] = self.adjMat[row+1][col]\n",
      "\n",
      "    def moveColLeft(self,col, length):\n",
      "        for row in range(self.length):\n",
      "            self.adjMat[row][col] = self.adjMat[row][col+1]\n",
      "\n",
      "theGraph = Graph()\n",
      "theGraph.addVertex('A') # 0\n",
      "theGraph.addVertex('B') # 1\n",
      "theGraph.addVertex('C') # 2\n",
      "theGraph.addVertex('D') # 3\n",
      "theGraph.addVertex('E') # 4\n",
      "theGraph.addVertex('F') # 5\n",
      "theGraph.addVertex('G') # 6\n",
      "theGraph.addVertex('H') # 7Connectivity in Directed Graphs\n",
      "theGraph.addEdge(0,3)\n",
      "theGraph.addEdge(0,4)\n",
      "theGraph.addEdge(1,4)\n",
      "theGraph.addEdge(2,5)\n",
      "theGraph.addEdge(3,6)\n",
      "theGraph.addEdge(4,6)\n",
      "theGraph.addEdge(5,7)\n",
      "theGraph.addEdge(6,7)\n",
      "theGraph.topo() # do the sort"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "ERROR: Graph has cycles\n"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}