cs324fig2.cdr Wednesday, September 9, 2020 9:43:17 AM Color profile: Disabled Graphs: The Basics Composite Default screen Implementation [Edge]target vertex
▲
Representing graphs in computers is not as Representation simple asthe more abstract math and picture representations. How to store in computer memory? Input formats How to inputvertex and edge info?
© 2020 Dr. Muhammad Al-Hashimi KAU CPCS-324 1 cs324fig2.cdr Wednesday, September 9, 2020 9:43:17 AM Color profile: Disabled Graph Implementation Composite Default screen Vertex Representation
Vertex Set 2 choices to implement (program) vertex set as a list: linked or indexed. N A vertex may be b a 0 a implemented as record or object a 1 b to store some d b property fields 2 c such as a label. c … c 3 d d …
Quiz What’s the difference … betweenaa set and list? Hint: at least 2.
© 2020 Dr. Muhammad Al-Hashimi KAU CPCS-324 2 cs324fig2.cdr Wednesday, September 9, 2020 9:43:17 AM Color profile: Disabled Graph Implementation Composite Default screen Edge Representation Incident edges
2 choices to implement edges:aa linked list or matrix (next). adjacency list vertex list b N a b c d a d b a d Each vertex stores incident c … a edge info in its own linked list. a ? Exercise Write a formal (math) … specification. Compare to adjacency list.
© 2020 Dr. Muhammad Al-Hashimi KAU CPCS-324 3 cs324fig2.cdr Wednesday, September 9, 2020 9:43:17 AM Color profile: Disabled Edge Representation Composite Default screen Adjacency Matrix
0 1 1 acdb b 0 0 a 1 1 1 a d 1 b 1 1 c … … c 1 vertex list d 1 1 a b c d b a d … c a d a b … Exercise Write the adjacency matrix. (Label clock- wise, answer last slide.)
© 2020 Dr. Muhammad Al-Hashimi KAU CPCS-324 4 cs324fig2.cdr Wednesday, September 9, 2020 9:43:17 AM Color profile: Disabled Edge Representation Composite Default screen Rationale Dense/sparse graphs
Quiz 5 What’s the maximum number of edges possi- ble in this graph? Draw the missing. 1 3
2 4
Exercise f Write an adjacency b list for each graph. Exercise Which one would be a d e g Compare the adjacency list considered sparse? and the adjacency matrix of each shown graph in terms of efficency of: a) storage, c h and b) add/remove edge ops.
© 2020 Dr. Muhammad Al-Hashimi KAU CPCS-324 5 cs324fig2.cdr Wednesday, September 9, 2020 9:43:17 AM Color profile: Disabled Graph Implementation Composite Default screen Outline
Ata minimum, a vertex object contains 2 fields: a Vertex objects label, and an adjacency list reference (=pointer). Lin“aanguage demo ssignment” page adjacent/target node Adjacency lists a b c d b a d Study linked list demo c a d a b Basic graph object Array ofvertex objects , each reference a linked list of target nodes © 2020 Dr. Muhammad Al-Hashimi KAU CPCS-324 6 cs324fig2.cdr Wednesday, September 9, 2020 9:43:17 AM Color profile: Disabled First Graph Object Composite Default screen
vertex list
this.label .adjacent ← null
b 0 a adjacency lists a d 1 b b c d a c … 2 c d a 3 d a Quiz b What does the self var … this refer to in each case? this.verts []
© 2020 Dr. Muhammad Al-Hashimi KAU CPCS-324 7 cs324fig2.cdr Wednesday, September 9, 2020 9:43:17 AM Color profile:Implementation Disabled Framework Composite Default screen