data structures, graphs
Post on 26-May-2015
572 Views
Preview:
TRANSCRIPT
DATA STRUCTURESMPHIL - GRAPHS
Jibrael Jos : Oct 2009
Avoid Taking Printout : Use RTF Outline in case needed 2
Graph OperationGraph StructureAdjacency ListCreate GraphInsert VertexInsert ArcRetrieve Vertex
Agenda
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
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
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
Please Do Not Take Printout : Use RTF Outline in case needed 6
Create Adjacency List
ChennaiBangalore
KolkattaMumbai Nagpur
Delhi
Hyderabad
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
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
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
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)
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
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
Avoid Taking Printout : Use RTF Outline in case needed 13
Minimum Spanning TreeShortest Path
Agenda
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
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
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
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
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)
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
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
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
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
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
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
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
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
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
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
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?
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
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)
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)
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
top related