33
DATA STRUCTURES MPHIL - GRAPHS Jibrael Jos : Oct 2009

Data Structures, Graphs

Embed Size (px)

Citation preview

Page 1: Data Structures, Graphs

DATA STRUCTURESMPHIL - GRAPHS

Jibrael Jos : Oct 2009

Page 2: Data Structures, Graphs

Avoid Taking Printout : Use RTF Outline in case needed 2

Graph OperationGraph StructureAdjacency ListCreate GraphInsert VertexInsert ArcRetrieve Vertex

Agenda

Page 3: Data Structures, Graphs

Please Do Not Take Printout : Use RTF Outline in case needed 3

Operations (6)

Add Vertex Delete Vertex Add Edge Delete Edge Find Vertex Traverse Graph

Page 4: Data Structures, Graphs

Please Do Not Take Printout : Use RTF Outline in case needed 4

Graph – Structure (2,6,2) graphHead

Count First

graphArc Dest nextArc

graphVertex nextVertex Data inDegree outDegree Processed Arc

Page 5: Data Structures, Graphs

Please Do Not Take Printout : Use RTF Outline in case needed 5

Adjacency List

A 1 B1 p

B 1 A3 p C E

C 2 E1 p

E 2 C1 p

Vertex List Adjacency List

4

Page 6: Data Structures, Graphs

Please Do Not Take Printout : Use RTF Outline in case needed 6

Create Adjacency List

ChennaiBangalore

KolkattaMumbai Nagpur

Delhi

Hyderabad

Page 7: Data Structures, Graphs

Please Do Not Take Printout : Use RTF Outline in case needed 7

Adjacency List

C 2 B2 p

D 2

D

1 p M

K 1 E1 p

M 2 B3 p

Vertex List

6

B 2 M2 p

N 1 D1 p

C

N

K

Page 8: Data Structures, Graphs

Please Do Not Take Printout : Use RTF Outline in case needed 8

createGraph

If (mem available) allocate (newPtr) newPtr->count=0 newPtr->first=null Else newPtr=null Return newPtr

Page 9: Data Structures, Graphs

Please Do Not Take Printout : Use RTF Outline in case needed 9

insertVertex

If (mem Not available) return -1

allocate (newPtr) Init all 6 Find Insertion Point

Empty Graph … at beginning Middle Before First Vertex

Page 10: Data Structures, Graphs

Please Do Not Take Printout : Use RTF Outline in case needed 10

insertArc

If (mem Not available) return -1

Locate from Locate to Insert New Arc

outDegree of from inDegree of to Allocate Set destination of new ptr to “toPtr” Set ptr of from to newPtr (explained in next page)

Page 11: Data Structures, Graphs

Please Do Not Take Printout : Use RTF Outline in case needed 11

insertArc

Find Insertion Point Empty List … at first arc Middle Before First Arc

Page 12: Data Structures, Graphs

Please Do Not Take Printout : Use RTF Outline in case needed 12

Retrieve Vertex

If Graph -> first null return -2 Else set curr to first loop till you find if found set data out return 1 else return -2

Page 13: Data Structures, Graphs

Avoid Taking Printout : Use RTF Outline in case needed 13

Minimum Spanning TreeShortest Path

Agenda

Page 14: Data Structures, Graphs

Please Do Not Take Printout : Use RTF Outline in case needed 14

Walk Thru

Minimum Spanning Tree Prim / Dijikstra (pg 579) Kruskal

Shortest Path Algorithm

Page 15: Data Structures, Graphs

Please Do Not Take Printout : Use RTF Outline in case needed 15

Minimum ST

ChennaiBangalore

KolkataMumbai Nagpur

Delhi

Hyderabad

5

3

5

2

34

3

3

Delhi: 5 or 3 D,N: 5,3,or 2 D,N,H:5,3,3 D,N,H,M:4,3 D,N,H,M,B:3 D,N,H,M,B,C:5 D,N,H,M,B,C,K

Page 16: Data Structures, Graphs

Please Do Not Take Printout : Use RTF Outline in case needed 16

Minimum ST

ChennaiBangalore

KolkataMumbai Nagpur

Delhi

Hyderabad

5

3

5

2

34

3

3

Start With Mumbai

Page 17: Data Structures, Graphs

Please Do Not Take Printout : Use RTF Outline in case needed 17

Network MST– Structure (2,7,4)

graphHead Count First

graphArc Dest inTree Weight nextEdge

graphVertex nextVertex Data inDegree outDegree inTree Processed * Edge

Page 18: Data Structures, Graphs

Please Do Not Take Printout : Use RTF Outline in case needed 18

M S Tree (pg 579)

Set inTree flags to false and when to true

Min Edge to Infinity (When and Why)

Min Edge Ptr (Why)

Loops (In order Vertex and Edge to initialise)

Loops (to find next Vertex and Edge to find)

Page 19: Data Structures, Graphs

Please Do Not Take Printout : Use RTF Outline in case needed 19

Adjacency List

C 2 B2 p

D 2

D

1 p M

K 1 E1 p

M 2 B3 p

Vertex List

6

B 2 M2 p

N 1 D1 p

C

N

K

Page 20: Data Structures, Graphs

Please Do Not Take Printout : Use RTF Outline in case needed 20

Shortest Path

Chennai∞

Bangalore∞

Kolkata∞

Mumbai : ∞

Nagpur∞

Delhi :∞

Hyderabad∞

5

3

5

2

34

3

3

Page 21: Data Structures, Graphs

Please Do Not Take Printout : Use RTF Outline in case needed 21

Shortest Path

Chennai∞

Bangalore∞

Kolkata∞

Mumbai : ∞

Nagpur∞

Delhi : 0

Hyderabad∞

5

3

5

2

34

3

3

Page 22: Data Structures, Graphs

Please Do Not Take Printout : Use RTF Outline in case needed 22

Shortest Path

Chennai∞

Bangalore∞

Kolkata∞

Mumbai : ∞

Nagpur3

Delhi : 0

Hyderabad∞

5

3

5

2

34

3

3

Page 23: Data Structures, Graphs

Please Do Not Take Printout : Use RTF Outline in case needed 23

Shortest Path

Chennai∞

Bangalore∞

Kolkata∞

Mumbai : ∞

Nagpur3

Delhi : 0

Hyderabad∞

5

3

5

2

34

3

3

5

65

Page 24: Data Structures, Graphs

Please Do Not Take Printout : Use RTF Outline in case needed 24

Shortest Path

Chennai∞

Bangalore∞

Kolkata∞

Mumbai : 5

Nagpur3

Delhi : 0

Hyderabad∞

5

3

5

2

34

3

3

Page 25: Data Structures, Graphs

Please Do Not Take Printout : Use RTF Outline in case needed 25

Shortest Path

Chennai∞

Bangalore∞

Kolkata∞

Mumbai : 5

Nagpur3

Delhi : 0

Hyderabad∞

5

3

5

2

34

3

3

9

5

Page 26: Data Structures, Graphs

Please Do Not Take Printout : Use RTF Outline in case needed 26

Shortest Path

Chennai∞

Bangalore∞

Kolkata∞

Mumbai : 5

Nagpur3

Delhi : 0

Hyderabad5

5

3

5

2

34

3

3

Page 27: Data Structures, Graphs

Please Do Not Take Printout : Use RTF Outline in case needed 27

Shortest Path

Chennai∞

Bangalore∞

Kolkata∞

Mumbai : 5

Nagpur3

Delhi : 0

Hyderabad5

5

3

5

2

34

3

3

98

Page 28: Data Structures, Graphs

Please Do Not Take Printout : Use RTF Outline in case needed 28

Shortest Path

Chennai∞

Bangalore8

Kolkata∞

Mumbai : 5

Nagpur3

Delhi : 0

Hyderabad5

5

3

5

2

34

3

3

Page 29: Data Structures, Graphs

Please Do Not Take Printout : Use RTF Outline in case needed 29

Shortest Path

Chennai11

Bangalore8

Kolkata16

Mumbai : 5

Nagpur3

Delhi : 0

Hyderabad5

5

3

5

2

34

3

3

What if we start from Hyderabad?

Page 30: Data Structures, Graphs

Please Do Not Take Printout : Use RTF Outline in case needed 30

Network SP– Structure (2,8,4)

graphHead Count First

graphArc Dest inTree Weight nextEdge

graphVertex nextVertex Data inDegree outDegree inTree pathLength Processed * Edge

Page 31: Data Structures, Graphs

Please Do Not Take Printout : Use RTF Outline in case needed 31

M S Tree (pg 583)

Set inTree flags to false and when to true

Path Length to Infinity (When and Why)

Min Edge Ptr (Why)

Page 32: Data Structures, Graphs

Please Do Not Take Printout : Use RTF Outline in case needed 32

M S Tree (pg 583)

Loop (In order Vertex) Loop (Edge to initialise) Loop Till Tree Complete Loop (to find next Vertex) Loop (locate smallest path) Test for shortest Path Set inTree (2 places) ,

pathLength (dest)

Page 33: Data Structures, Graphs

Please Do Not Take Printout : Use RTF Outline in case needed 33

Reference (PS: starting vertex only) http://

en.wikipedia.org/wiki/Graph_theory http://en.wikipedia.org/wiki/

Dijkstra’s_algorithm Furzon (for those who have no earlier

graph background): Chapter 12 Action Item