1 / 54

Graph

Graph. Dr. Bernard Chen Ph.D. University of Central Arkansas. Graph Algorithms. Graphs and Theorems about Graphs Graph Algorithms minimum spanning tree. What can graphs model?. Cost of wiring electronic components together. Shortest route between two cities.

mieko
Download Presentation

Graph

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. Graph Dr. Bernard Chen Ph.D. University of Central Arkansas

  2. Graph Algorithms • Graphs and Theorems about Graphs • Graph Algorithms • minimum spanning tree

  3. What can graphs model? • Cost of wiring electronic components together. • Shortest route between two cities. • Finding the shortest distance between all pairs of cities in a road atlas.

  4. What is a Graph? • Informally a graph is a set of nodes joined by a set of lines or arrows. 1 2 3 1 3 2 4 4 5 6 5 6

  5. Directed Graph • A directed graph is a pair ( V, E ), where the set V is a finite set and E is a binary relation on V . • The set V is called the vertex set of G and the elements are called vertices. • The set E is called the edge set of G and the elements are edges

  6. Directed Graph Self loop Isolated node 1 3 7 2 4 5 6 V = { 1, 2, 3, 4, 5, 6, 7 }| V | = 7 E = { (1,2), (2,2), (2,4), (4,5), (4,1), (5,4),(6,3) }| E | = 7

  7. Undirected Graph • An undirected graph G = ( V, E ) , but unlike a digraph the edge set E consist of unordered pairs. • We use the notation (a,b ) to refer to a directed edge, and { a, b } for an undirected edge.

  8. Undirected Graph V = { A, B, C, D, E, F } |V | = 6 A B C E = { {A, B}, {A,E}, {B,E}, {C,F} } |E | = 4 D F E Some texts use (a, b) also for undirected edges. So ( a, b ) and ( b, a ) refers to the same edge.

  9. Degree • Degree of a Vertex in an undirected graph is the number of edges incident on it. • In a directed graph , the out degree of a vertex is the number of edges leaving it and the in degree is the number of edges entering it.

  10. Degree The degree of B is 2. A B C D F E

  11. Degree The in degree of 2 is 2 andthe out degree of 2 is 3. 1 2 4 5

  12. Weighted Graph • A weighted graph is a graph for which each edge has an associated weight, usually given by a weight functionw: E R.

  13. Weighted Graph 1.2 1 3 2 .2 1.5 .5 .3 4 5 6 .5

  14. Implementation of a Graph • Adjacency-list representation of a graph G = ( V, E ) consists of an array ADJ of |V | lists, one for each vertex in V. For each uV , ADJ [ u ] points to all its adjacent vertices.

  15. Implementation of a Graph 2 5 1 1 2 2 1 5 3 4 3 3 2 4 5 4 4 2 5 3 5 4 1 2

  16. Implementation of a Graph 2 5 1 1 2 2 5 3 4 3 3 4 5 4 4 5 5 5

  17. Adjacency lists • Advantage: • Saves space for sparse graphs. Most graphs are sparse. • “Visit” edges that start at v • Must traverse linked list of v • Size of linked list of v is degree(v) • (degree(v))

  18. Adjacency-matrix-representation • Adjacency-matrix-representation of a graph G = ( V, E) is a |V | x |V | matrix A = ( aij ) • such that aij = 1 (or some Object) if (i, j ) E 0 (or null) otherwise.

  19. Adjacency-matrix-representation 0 1 2 3 4 0 1 2 0 1 0 0 1 0 1 2 3 4 1 0 1 1 1 4 3 0 1 0 1 0 0 1 1 0 1 1 1 0 1 0

  20. Adjacency-matrix-representation 0 1 2 3 4 0 1 0 0 1 0 1 0 1 2 3 4 0 0 1 1 1 2 0 0 0 1 0 4 0 0 0 0 1 3 0 0 0 0 0

  21. Adjacency-matrix-representation • Advantage: • Saves space on pointers for dense graphs, and on • small unweighted graphs using 1 bit per edge. • Check for existence of an edge (v, u) • (adjacency [i] [j]) == true?) • So (1)

  22. Graph Algorithms • Graphs and Theorems about Graphs • Graph Algorithms • minimum spanning tree

  23. Minimum Spanning Tree

  24. Example of MST

  25. Problem: Laying Telephone Wire Central office

  26. Wiring: Naïve Approach Central office Expensive!

  27. Wiring: Better Approach Central office Minimize the total length of wire connecting the customers

  28. Growing an MST: general idea • GENERIC-MST(G,w) • A{} • while A does not form a spanning tree • do find an edge (u,v) that is safe for A • A A U {(u,v)} • return A

  29. Tricky part • How do you find a safe edge? • This safe edge is part of the minimum spanning tree

  30. Algorithms for MST • Prim’s • Grow a MST by adding a single edge at a time • Kruskal’s • Choose a smallest edge and add it to the forest • If an edge is formed a cycle, it is rejected

  31. Prim’s greedy algorithm • Start from some (any) vertex. • Build up spanning tree T, one vertex at a time. • At each step, add to T the lowest-weight edge in G that does not create a cycle. • Stop when all vertices in G are touched

  32. Prim’s MST algorithm

  33. Example C 4 1 B F 2 7 3 3 2 E A I 4 6 7 5 H D 9 8 G

  34. = in heap Min EdgePick a root C 4 1 B F 2 7 3 3 2 E A I 4 6 7 5 H D 9 8 G

  35. Min Edge = 1 C 4 1 B F 2 7 3 3 2 E A I 4 6 7 5 H D 9 8 G

  36. Min Edge = 2 C 4 1 B F 2 7 3 3 2 E A I 4 6 7 5 H D 9 8 G

  37. Min Edge = 2 C 4 1 B F 2 7 3 3 2 E A I 4 6 7 5 H D 9 8 G

  38. Min Edge = 3 C 4 1 B F 2 7 3 3 2 E A I 4 6 7 5 H D 9 8 G

  39. Min Edge = 4 C 4 1 B F 2 7 3 3 2 E A I 4 6 7 5 H D 9 8 G

  40. Min Edge = 3 C 4 1 B F 2 7 3 3 2 E A I 4 6 7 5 H D 9 8 G

  41. Min Edge = 4 C 4 1 B F 2 7 3 3 2 E A I 4 6 7 5 H D 9 8 G

  42. Min Edge = 6 C 4 1 B F 2 7 3 3 2 E A I 4 6 7 5 H D 9 8 G

  43. Example II

  44. Kruskal’s Algorithm • Choose the smallest edge and add it to a forest • Keep connecting components until all vertices connected • If an edge would form a cycle, it is rejected.

  45. Example C 4 1 B F 2 7 3 3 2 E A I 4 6 7 5 H D 9 8 G

  46. Min Edge = 1 C 4 1 B F 2 7 3 3 2 E A I 4 6 7 5 H D 9 8 G

  47. Min Edge = 2 C 4 1 B F 2 7 3 3 2 E A I 4 6 7 5 H D 9 8 G

  48. Min Edge = 2 C 4 1 B F 2 7 3 3 2 E A I 4 6 7 5 H D 9 8 G

  49. Min Edge = 3 C 4 1 B F 2 7 3 3 2 E A I 4 6 7 5 H D Now have 2 disjoint components: ABFG and CH 9 8 G

  50. Min Edge = 3 C 4 1 B F 2 7 3 3 2 E A I 4 6 7 5 H D 9 8 G

More Related