Graph Implementation Composite Default Screen Vertex Representation
Total Page:16
File Type:pdf, Size:1020Kb
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 W 1 1 1 a d 1 b 1 W W 1 c … … c 1 W W W vertex list d 1 1 W W 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 Development environment Javascript, Firefox + Dev Tools Coding conventions Object and source organization Code caller (html) page Link “modules”, generate output © 2020 Dr. Muhammad Al-Hashimi KAU CPCS-324 8 cs324fig2.cdr Wednesday, September 9, 2020 9:43:17 AM Color profile: Disabled Implementation Framework Composite Default screen Walkthrough © 2020 Dr. Muhammad Al-Hashimi KAU CPCS-324 9 cs324fig2.cdr Wednesday, September 9, 2020 9:43:17 AM Color profile: Disabled Programming Checkpoint Composite Default screen Checkpoint 1 expectations First graph object,starter code in “assignment” page Fill in code where indicated Checkupload guide carefully Weekly workload 2–– 3 hrs/credit, 15 16 credit semester © 2020 Dr. Muhammad Al-Hashimi 3-4 KAU CPCS-324 10 cs324fig2.cdr Wednesday, September 9, 2020 9:43:17 AM Color profile: Disabled Graph Implementation Composite Default screen Outline Vertex objects Language demo in “assignment” page adjacent/target node Ata minimum, an adjacent/ target node Adjacency lists storesaoftarget vertex a b c d an incident edge, and b a d a reference to next node Study linked list demo c a in a linked list. 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 11 cs324fig2.cdr Wednesday, September 9, 2020 9:43:18 AM Color profile: Disabled Graph Implementation Composite Default screen Edge Implementation & Figure 3.10 g h A naive implementation a e Targetvertices can be c f listed in any order, a d b separator maybe comma or similar. j i 0 a c d e 2 3 4 1 b e f 4 5 2 c a d f ? 3 d a c 4 e a b f 5 f b c e 6 g h i Exercise 7 h g i Discuss at least 2 ? disadvantages of the 8 i h j naive input format. 9 j g i Disadvantages © 2020 Dr. Muhammad Al-Hashimi KAU CPCS-324 12 cs324fig2.cdr Wednesday, September 9, 2020 9:43:18 AM Color profile: Disabled Graph Implementation Composite Default screen Input Formats XML-based formats GraphML, GXL <>graph id="G " edgedefault="undirected " </>node id="n " </>node id="n1 " </>edge id="e1 " source=" n " target=" n1 " </graph > v Makkah v1 Jeddah v2 Madinah Plain-text/key-value formats v3 Taif v4 Yanbu N # TGF,LEDA , GML, JSON v v1 79.83 v1 v 79.75 v1 v3 13. v2 v1 38.65 © 2020 Dr. Muhammad Al-Hashimi KAU CPCS-324 13 cs324fig2.cdr Wednesday, September 9, 2020 9:43:19 AM Color profile: Disabled Edge Implementation Composite Default screen Better Edge List Input Edge list specification Re-implement input interface Need edge type specification Some add-edge interface to hide insert detail © 2020 Dr. Muhammad Al-Hashimi KAU CPCS-324 14 cs324fig2.cdr Wednesday, September 9, 2020 9:43:19 AM Color profile: Disabled First Edge Implementation Composite Default screen adjacency lists this.adjacent ←? 1 b 3 0 a 1 b 2 c 3 d a d 1 b a d c … 2 c a 2 … target nodes this.verts[] A minimum target node contains array Simple node index of adjacent vertex. Review linked list package Ñ It’s more important to learn how to use the package at this Note object interfaces stage. Call examples © 2020 Dr. Muhammad Al-Hashimi KAU CPCS-324 15 cs324fig2.cdr Wednesday, September 9, 2020 9:43:19 AM Color profile: Disabled Programming Checkpoint Composite Default screen Checkpoint 2 Clean up needed parts of your code from previous Checkpoint expectations starter file and paste it into the new one. First edge implementation, starter 2 Usenew code starter Fill under outline comments Review upload guide Answers 1 234 1 2 Demo some basic Git moves 4 3 © 2020 Dr. Muhammad Al-Hashimi KAU CPCS-324 16.