1 / 134

Introduction to Graphs and Graph Representations

This text provides an introduction to graphs, different types of graphs, and their uses in real-life scenarios. It also explains various graph representations, such as adjacency lists and adjacency matrices.

pnash
Télécharger la présentation

Introduction to Graphs and Graph Representations

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. Graphs David Kauchak cs161 Summer 2009

  2. Administrative • HW grading • Midterms returned • New TA • Tiyu Wang • Additional office hours Tue. 7-10pm • HW4 • Feedback – Thanks!

  3. Midterm

  4. “on-disk” sorting

  5. A B C E D F G Graphs • A graph is a set of vertices V and a set of edges (u,v)  E where u,v  V

  6. A B C E D F G Different types of graphs • Undirected – edges do not have a direction

  7. A B C E D F G Different types of graphs • Directed – edges do have a direction

  8. A B C E D F G Different types of graphs • Weighted – edges have an associated weight 8 7 7 2 1 20 2

  9. A B C E D F G Different types of graphs • Weighted – edges have an associated weight 8 7 7 2 1 20 2

  10. A B C E D F G Terminology • Path – A path is a list of vertices p1,p2,…pk where there exists an edge (pi,pi+1)  E

  11. A B C E D F G Terminology • Path – A path is a list of vertices p1,p2,…pk where there exists an edge (pi,pi+1)  E {A, B, D, E, F}

  12. A B C E D F G Terminology • Path – A path is a list of vertices p1,p2,…pk where there exists an edge (pi,pi+1)  E {C, D}

  13. A B C E D F G Terminology • Cycle – A path p1,p2,…pk where p1 = pk

  14. A B C E D F G Terminology • Cycle – A path p1,p2,…pk where p1 = pk {A, B, D}

  15. A B C E D F G Terminology • Cycle – A path p1,p2,…pk where p1 = pk

  16. A B C E D F G Terminology • Cycle – A path p1,p2,…pk where p1 = pk not a cycle

  17. A B C E D F G Terminology • Cycle – A path p1,p2,…pk where p1 = pk cycle

  18. A B C E D F G Terminology • Connected – every pair of vertices is connected by a path connected

  19. A B C E D F G Terminology • Connected (undirected graphs) – every pair of vertices is connected by a path not connected

  20. A B C E D F G Terminology • Strongly connected (directed graphs) – Every two vertices are reachable by a path not strongly connected

  21. A B E D F G Terminology • Strongly connected (directed graphs) – Every two vertices are reachable by a path not strongly connected

  22. A B E D F G Terminology • Strongly connected (directed graphs) – Every two vertices are reachable by a path strongly connected

  23. A B C E D F G H Different types of graphs • Tree – connected, undirected graph without any cycles

  24. A B C E D F G H Different types of graphs • Tree – connected, undirected graph without any cycles need to specify root

  25. A B C E D F G H Different types of graphs • Tree – connected, undirected graph without any cycles

  26. A B C E D F G H Different types of graphs • DAG – directed, acyclic graph

  27. A B C D F Different types of graphs • Complete graph – an edge exists between every node

  28. A B C E D F G Different types of graphs • Bipartite graph – a graph where every vertex can be partitioned into two sets X and Y such that all edges connect a vertex u  X and a vertex v  Y

  29. When do we see graphs in real life problems? • Transportation networks (flights, roads, etc.) • Communication networks • Web • Social networks • Circuit design • Bayesian networks

  30. A B C E D Representing graphs • Adjacency list – Each vertex u  V contains an adjacency list of the set of vertices v such that there exists an edge (u,v)  E A: B D B: A D C: D D: A B C E E: D

  31. A B C E D Representing graphs • Adjacency list – Each vertex u  V contains an adjacency list of the set of vertices v such that there exists an edge (u,v)  E A: B B: C: D D: A B E: D

  32. A B C E D Representing graphs • Adjacency matrix – A |V|x|V| matrix A such that: A B C D E A 0 1 0 1 0B 1 0 0 1 0C 0 0 0 1 0D 1 1 1 0 1E 0 0 0 1 0

  33. A B C E D Representing graphs • Adjacency matrix – A |V|x|V| matrix A such that: A B C D E A 0 1 0 1 0B 1 0 0 1 0C 0 0 0 1 0D 1 1 1 0 1E 0 0 0 1 0

  34. A B C E D Representing graphs • Adjacency matrix – A |V|x|V| matrix A such that: A B C D E A 0 1 0 1 0B1 0 0 1 0C 0 0 0 1 0D 1 1 1 0 1E 0 0 0 1 0

  35. A B C E D Representing graphs • Adjacency matrix – A |V|x|V| matrix A such that: A B C D E A 0 1 0 1 0B 1 0 0 1 0C 0 0 0 1 0D1 1 1 0 1E 0 0 0 1 0

  36. A B C E D Representing graphs • Adjacency matrix – A |V|x|V| matrix A such that: A B C D E A 0 1 0 1 0B 1 0 0 1 0C 0 0 0 1 0D 1 1 1 0 1E 0 0 0 1 0 Is it always symmetric?

  37. A B C E D Representing graphs • Adjacency matrix – A |V|x|V| matrix A such that: A B C D E A 0 1 0 0 0B 1 0 0 0 0C 0 0 0 1 0D 1 1 0 0 0E 0 0 0 1 0

  38. Adjacency list vs.adjacency matrix • Sparse graphs (e.g. web) • Space efficient • Must traverse the adjacency list to discover is an edge exists Adjacency list Adjacency matrix • Dense graphs • Constant time lookup to discover if an edge exists • simple to implement • for non-weighted graphs, only requires boolean matrix Can we get the best of both worlds?

  39. A B C E D Sparse adjacency matrix • Rather than using an adjacency list, use an adjacency hashtable A: hashtable [B,D] B: hashtable [A,D] C: hashtable [D] D: hashtable [A,B,C,E] E: hashtable [D]

  40. A B C E D Sparse adjacency matrix • Constant time lookup • Space efficient • Not good for dense graphs A: hashtable [B,D] B: hashtable [A,D] C: hashtable [D] D: hashtable [A,B,C,E] E: hashtable [D]

  41. A B C E D Weighted graphs • Adjacency list • store the weight as an additional field in the list A: B:8 D:3 8 3 13 2 10

  42. A B C E D Weighted graphs • Adjacency matrix A B C D E A 0 8 0 3 0B 8 0 0 2 0C 0 0 0 10 0D 3 2 10 0 13E 0 0 0 13 0 8 3 13 2 10

  43. Graph algorithms/questions • Graph traversal (BFS, DFS) • Shortest path from a to b • unweighted • weighted positive weights • negative/positive weights • Minimum spanning trees • Are all nodes in the graph connected? • Is the graph bipartite? • hw4 

  44. Breadth First Search (BFS) on Trees

  45. A B C E D F G Tree BFS Q:

  46. A B C E D F G Tree BFS Q: A

  47. A B C E D F G Tree BFS Q:

  48. A B C E D F G Tree BFS Q: B, D, E

  49. A B C E D F G Tree BFS Q: D, E

  50. A B C E D F G Tree BFS Q: D, E, C, F

More Related