1 / 38

Introduction to Graphs

Introduction to Graphs. Graphs: what are they?. Representations of pairwise relationships Collections of objects under some specified relationship. Graphs: what are they mathematically?. A graph G is a pair (V,E) V is a set of vertices (nodes) E is a set of pairs ( a,b ), a,b  V

espen
Download Presentation

Introduction to Graphs

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. Introduction to Graphs

  2. Graphs: what are they? • Representations of pairwise relationships • Collections of objects under some specified relationship

  3. Graphs: what are they mathematically? • A graph G is a pair (V,E) • V is a set of vertices (nodes) • E is a set of pairs (a,b), a,b V • V is the set of relatable objects • E is the set of relationships

  4. A Visual Example 1 3 2 4 5 G = ( {1,2,3,4,5}, {(1,2), (1,4), (2,3), (2,4), (1,5)} )

  5. Directed Graphs • In a directed graph • (a,b)  E does not imply (b,a)  E • Undirected graphs are a subset • (a,b)  E if and only if (b,a)  E • Visually, directed graphs are drawn with arrows

  6. Directed Graph Example 1 3 2 4 5 G = ( {1,2,3,4,5}, { (1,5), (2,1), (2,3), (2,4), (3,2), (4,1) } )

  7. Weighted Graphs • Have weights associated with edges • Can be directed or undirected • Can have pairs, in a directed graph, where the weights from (a,b) have no relationship on the weights from (b,a)

  8. Weighted Graph Example 1 3.2 3 2 π 42 666 -5 777 4 5 G = ( {1,2,3,4,5}, { (1,5,-5), (2,1,3.2), (2,3,42), (3,2, π), (2,4,777), (4,1,666) } )

  9. Graph Applications

  10. Graph Representation • How to represent in memory? • Two common ways: • Adjacency Lists • Adjacency Matrix

  11. Undirected Graph Example G = ( {1,2,3,4,5}, {(1,2), (1,4), (2,3), (2,4), (1,5)} ) 1 2 3 4 5 2 4 5 1 3 4 2 1 2 1

  12. Directed Graph Example G = ( {1,2,3,4,5}, { (1,5), (2,1), (2,3), (2,4), (3,2), (4,1) } ) 1 2 3 4 5 5 1 3 4 2 1

  13. Undirected Graph Example G = ( {1,2,3,4,5}, {(1,2), (1,4), (2,3), (2,4), (1,5)} )

  14. Directed Graph Example G = ( {1,2,3,4,5}, { (1,5), (2,1), (2,3), (2,4), (3,2), (4,1) } )

  15. Cycles

  16. Trees

  17. Graph Searching Algorithms

  18. DFS Algorithm • Algorithm DFS ( G ) for i = 1 to n do // Initialize all vertices are unvisited status[i] = unvisited parent[i] = NULL for i = 1 to n do if (status[i] == unvisited) // If there exits an invisited vertex, start traversal DF-Travel(i) Algorithm DF-Travel ( v ) status[v] = visited for each vertex u adjacent to v do if status[u] == unvisited then parent[u] = v DF-Travel ( u )

  19. Breadth First Search Algorithm • Breadth First Search (BSF): • Initially all vertices of the graph are unvisited. • Start visiting the graph from any vertex, say v . • Visit each unvisited adjacent vertex of v. • Repeat the process for each vertex visited. • This process stops when all vertices reachable from v are visited. • This method is called breadth first search, since it works outward from a center point, much like the ripples created when throwing a stone into a pond. It moves outward in all directions, one level at a time.

  20. What is the Minimum Spanning tree (MST)? • Consider Oil Well example.=> • Resultant graph is a tree which spannes all vertices of the graph. Hence called spanning tree.    •  The spanning tree with minimum cost is called Minimum Spanning tree (MST).

  21. Example

  22. Minimum Spanning Tree

  23. Minimum Spanning Tree • Required: Connect all nodes at minimum cost. • Cost is sum of edge weights • Can start at any node • Unique solution. • Can come from different sets of edges • Two algorithms • Prim’s • Kruskal’s

  24. Finding a MST • Principal greedy methods: algorithms by Prim and Kruskal • Prim • Grow a single tree by repeatedly adding the least cost edge that connects a vertex in the existing tree to a vertex not in the existing tree • Kruskal • Grow a tree by repeatedly adding the least cost edge that does not introduce a cycle among the edges included so far

  25. Prim’sAlgorithm Start with any vertex. All its edges are candidates. Stop looking when all vertices are picked Otherwise repeat… Pick minimum edge (no cycles) and connect the adjacent vertex Add all edges from that new vertex to our “candidates”

  26. The Shortest Path Problem • Given a directed, weighted graphG = ( V, E ) • What is the shortest path from the start vertex to some end vertex? • Minimize the sum of the edge weights

  27. Dijkstra's Shortest Path Algorithm • In a graph in which edges have costs .. • Find the shortest path from a source to a destination Surprisingly .. • While finding the shortest path from a source to one destination, we can find the shortest paths to all over destinations as well !

  28. Dijkstra Algorithm • Problem: • From a given source vertex s ∈ V, find the shortest-path weights d(s, v)for all v∈V. • IDEA: Greedy. • Maintain a set S of vertices whose shortest-path distances from s are known. • At each step add to S the vertex v ∈ V –S whose distance estimate from s is minimal. • Update the distance estimates of vertices adjacent to v.

  29. Dijkstra's Shortest Path Algorithm • For a graph G = ( V, E ) • Dijkstra’s algorithm keeps two sets of vertices: • S Vertices whose shortest paths have already been determined • V-S Remainder • Also • d Best estimates of shortest path to each vertex • Π Predecessors for each vertex

  30. Dijkstra's Shortest Path Algorithm • Find shortest path from s to t.

  31. Thanks

More Related