1 / 23

Last Tutorial 11

Last Tutorial 11. Graph. Graph – What is it?. G(V,E) Collection of vertices V and edges E Not a totally new data structure, this is an extension from Linked List  Tree Very useful to model many (real-life) problems Internet Road Traffic Many others… Basic Terminologies (see q1 !)

dash
Download Presentation

Last Tutorial 11

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Last Tutorial 11 Graph

  2. Graph – What is it? • G(V,E) • Collection of vertices V and edges E • Not a totally new data structure, this is an extension from Linked List  Tree • Very useful to model many (real-life) problems • Internet • Road Traffic • Many others… • Basic Terminologies (see q1!) • Vertices, Edges • Path, Cycle • Directed/Un-directed • Weighted/Un-weighted • Connected/Un-connected • In-degree/Out-degree

  3. Graph – Basic Representations • Adjacency Matrix • 2-D array of vertices • M[i][j] = 1 if there is an edge between vertex i and j, 0 otherwise • For weighted graph, M[i][j] is the weight of the edges, not just 0 or 1! • Adjacency List • An array of V linked lists • List i represent the vertices connected to vertex i • For weighted graph, the list store both neighboring vertices and their edge costs • Discussed in Q1 • There are more representations • But we do not have time to discuss all…

  4. Student Presentation • Gr3 (average 2 times) • Gr4 (average 3 times) Overview of the questions: • Graph representations! (>1 students) • Graph traversals! (>1 students) • Topological Sort (1 student) • Mix and Match (floor) • Gr5 (average 4 times) • Gr6 (average 3 times)

  5. Q1 – Graph Representations • Explain: • Characteristics of the Graph • directed, weighted, connected*, has cyclenot dense, not too sparse either • Adjacency List • Adjacency Matrix • For small graph like this, both representations are ~ equally good. • SGUK (10 days)SGTokyo is cheaper than SGTokyo (10 days) HUB (3 in, 4 out) Dead end (1 in, 0 out) Weight can also be associated with vertex!

  6. Graph – Basic Algorithms (1) • Graph traversal algorithms: • Breadth First Search (BFS) O(V+E) • O(V + E) is to say “take the max between V or E”.For sparse graph, E < V.For dense graph, E can be V2! • Visit the graph breadth first • Usually implemented using queue • Depth First Search (DFS) O(V+E) • Visit the graph depth first • Usually implemented using recursion/stack • Discussed in Q2

  7. Q2 – Graph Traversals • Start from Singapore: • DFS • Answer: Singapore  Bali (cheapest),backtrack  Tokyo  Shanghai,backtrack, backtrack  London • BFS • Answer: It happens to be the same sequence:Singapore  Bali (cheapest)  Tokyo  Shanghai  London • If there is an edge (direct flight) of cost 9K from London to Shanghai • And we start from London • DFS • London  Shanghai  Tokyo  Singapore  Bali • BFS • London  (starting)Shanghai  Singapore  (1st layer)Tokyo  Bali (2nd layer) 9K

  8. Graph – Basic Algorithms (2) • Topological Sort • Topological Order: • Linear order that does not violate precedence constraint,e.g. underwear  outerwear * • The algorithm: O(V+E) • Insert vertex (or vertices) v with in-degree 0 in a queue • While queue is not empty() • Take/process vertex v located in front of the queue • Decrease the in-degree of vertices adjacent to v by 1 • If in-degree becomes 0, add it into the queue • Discussed in Q3

  9. Q3 – Topological Sort • Valid topological order (starting from Australian GP): • Australian GP, Malaysian GP, Monaco GP, French GP, British GP, Italian GP, Belgian GP, Singapore GP, Japanese GP, Chinese GP, Abu Dhabi GP • If Chinese GP  Japanese GP (a “funny” constraint) • We cannot use topological sort! (Graph must be Directed Acyclic Graph/DAG) Note: This constraint graph happens to be a DAG. However, not all constraint graphs are DAGs!

  10. Graph – Basic Algorithms (3) Shortest Path algorithm (not covered in CS1102 syllabus this sem) If the graph is un-weighted We can run BFS(), the level is the cost of the path O(V+E) If the graph is weighted Use Dijkstra Shortest Path Algorithm O((V+E) log V)

  11. Graph – Basic Algorithms (4) • There are many others… • We cannot study all in this CS1102 module… • Those who are interested to study more about Graph and other advanced algorithms like Dynamic Programming, Greedy Algorithm, etc should take CS3230 – Design and Analysis of Algorithms

  12. Q4 – Mix and Match (1)

  13. Q4 – Mix and Match (2)

  14. Q4 – Mix and Match (3)

  15. Our Class Photo  • You can see our photos here: • http://www.comp.nus.edu.sg/~stevenha/myteaching/record.html

  16. Advertisement

  17. Food For Thought • This is ‘the end’ of CS1102 tutorial • Remember that you still have final exam! • I hope you enjoy studying this module • Regardless your final grade :$ • All the best for your final exam and future endeavor • We may/may not meet again in the future • But if we do, please greet each other  • PS: Hopefully you are not in CS1102 class again next semester… • Take CS3230 if you are interested to study advanced algorithms

  18. Questions from Last Semester • Use the remaining slides as extra exercises!

  19. Q1 – Graph Representation Show this graph’s  Adjacency List: Adjacency Matrix:

  20. Q2 – Adjacency Matrix or List?? Would you use the adjacency list structure or the adjacency matrix structure in each of the following cases? Justify your choice. • The graph has 10,000 vertices and 20,000 edges.It is important to use as little space as possible. Answer: Use adjacency list as there are on average 2 edges on each vertex.We will waste a lot of memory space if adjacency matrix is used. • The graph has 10,000 vertices and 20,000,000 edges.It is important to use as little space as possible. Answer: Use adjacency matrix as there are on average 2000 edges on each vertex.It would be much faster to find the neighbors of each vertex. • You need to answer the query areAdjacent as fast as possible.No matter how much space you use. Answer: Use adjacency Matrix since space is not a problem.More importantly, the query areAdjacent can be answered in O(1) time. 

  21. Q3 – `Slow’ DFS?? Explain why the DFS traversal runs in O(n2) time On an n-vertex simple graph that is represented with the adjacency matrix structure. Answer: This is because for each vertex, We need to look at (n – 1) entries to see if they are the adjacent vertices. Using Adjacency List, this DFS runs in O(V+E) PS: A little note about V+E The + sign means: whoever greater takes over this asymptotic value. In sparse graph: E can be < V, O(V+E) = O(V) In dense graph: E can be as many as V2, thus E >> V, O(V+E) = O(V+V2) = O(V2)

  22. Q4 – DFS, BFS, Topological Sort • Do DFS and BFS starting from vertex `a’. • DFS/BFS traversal depends onthe ordering of neighbors… • Any valid DFS/BFS is accepted. • But usually we order neighbor alphabetically. • DFS: • a, b, c, d, f, e, g, i, h (my default answer) OR • a, b, c, d, h, f, g, e, i (if you adopt other vertex ordering) OR • a, b, c, e, i, g, d, h, f OR other possible sequences • BFS: • a, b, c, d, e, f, h, g, i (my default answer) OR • a, d, c, b, h, f, e, g, i (if you adopt other vertex ordering)OR other possible sequences • Note that a, b, c, d, f, e, h, g, i is not an acceptable sequence, why? • We are using a queue, e will be inside first after visiting c, so e is executed first before f… • Homework: • Try doing DFS/BFS from other nodes • Write the topological order of the vertices for the graph given above. • a, b, c, d, f, h, e, g, i (my default answer) OR • a, b, c, d, h, f, e, g, i (if you adopt other vertex ordering)

  23. Q5 – Dijkstra • Do Shortest Path from `a’ using Dijkstra’s algorithm! • Homework: • Try doing Shortest Path from ‘b’, ‘c’, ‘d’, etc…

More Related