1 / 133

Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees

Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems. Graph Definitions. A graph G is denoted by G = (V, E) where V is the set of vertices or nodes of the graph E is the set of edges or arcs connecting the vertices in V

gavin
Download Presentation

Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees

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 Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems

  2. Graph Definitions A graph G is denoted by G = (V, E) where V is the set of vertices or nodes of the graph E is the set of edges or arcs connecting the vertices in V Each edge E is denoted as a pair (v,w) where v,we V For example in the graph below 1 2 5 6 4 3 V = {1, 2, 3, 4, 5, 6} E = {(1, 2) (2, 5) (3, 6) (4, 6) (5, 6)} • This is an example of an unordered or undirected graph

  3. Graph Definitions Complete Graph A graph is complete, if every possible edge exists

  4. Graph Definitions (contd.) If the pair of vertices is ordered then the graph is a directed graph or a di-graph 1 2 5 6 4 3 Here, V = {1, 2, 3, 4, 5, 6} E = {(1, 2) (2, 5) (5, 6) (6, 3) (6, 4)} • Vertex v is adjacent to w iff (v,w) e E • Sometimes an edge has another component called a weight or cost. If the weight is absent it is assumed to be 1

  5. Graph Definitions (contd.) 1 2 5 6 4 3 4 1 2.5 2 1.2 Here, V = {1, 2, 3, 4, 5, 6} E = {(1, 2,4) (2, 5,1) (5, 6,1.2) (6, 3,2.5) (6, 4,2)}

  6. a b c d a a b b c c d d Simple path: a, e, k, p, l, q m, h, d, c, g e f f f f h g e e f f h h g g (no repeated vertices) m j k l m m j j k k l l p q n o p p q q n n o o Path A path of length k is a sequence v , v , …, v of vertices such that (v , v ) for i = 0, 1, …, k – 1 is an edge of G. 0 1 k i i+1 b, c, d not a path a b b b c c c d d d d e e e e f f f f f f f f f f f f f f f f h h h h g g g g Non-simple path: a, b, e, f, g, b, g, l m m m m j j j j k k k k l l l l p p p p q q q q n n n n o o o o

  7. Cycle A cycle is a path that starts and ends at the same vertex. A simple cycle has no repeated vertices. a b c d e f h g m j j j j j j j j k l k, j, n, k, p, o,k is not simple. p q n n n n n n o o

  8. Subgraph A subgraphH of G is a graph; its edges and vertices are subsets of those of G. a b c d e f h g m j k l p q n o V(H) = {b, d, e, f, g, h, l, p, q} E(H) = {(b, e), (b, g), (e, f), (d, h), (l, p), (l, q)}

  9. a d f g b c f e C 3 C 2 a C d e 1 b c Connectivity G is connected if there is a path between every pair of vertices. If G is not connected, the maximal connected subgraphs are the connected componentsof G.

  10. It is weakly connected if the underlying undirected graph is connected. c f a b e d Strong & Weak Connectivity A directed graph is strongly connected if every two vertices are reachable from each other. b a e f d c

  11. Tree, Forest • Tree is a graph in which any two vertices are connected by exactly one path or • any connected graph with no cycles is a tree. • A forest is a disjoint union of trees.

  12. If G is connected, then | E | ≥ | V | – 1. If G is a tree, then | E | = | V | – 1. If G is a forest, then | E | ≤ | V | – 1. Property of Connectivity Let G = (V, E) be an undirected graph.

  13. Degree In an undirected graph, the degree of a vertex is the number of edges that are attached to that vertex. Each vertex has degree = 3

  14. Degree cont. In a directed graph, we have In-degree: # of edges that end at that vertex Out-degree: # of edges that originate at that vertex 1 2 5 4 3 2 3 1 2 3 4 1 2 3 4 1 2 3 6

  15. Applications of Graphs Driving Map Edge = Road Vertex = Intersection Edge weight = Time required to cover the road Airline Traffic Vertex = Cities serviced by the airline Edge = Flight exists between two cities Edge weight = Flight time or flight cost or both Computer networks Vertex = Server nodes Edge = Data link Edge weight = Connection speed

  16. George Paul Linda Ringo Yoko John Applications of Graphs Graphs describe relationships Molecules Flow Charts Social Networks Geometric Surfaces (CAD) Circuits Parts in an Assembly …

  17. Representing Graphs: Adjacency Matrix Adjacency Matrix Two dimensional matrix of size n x n where n is the number of vertices in the graph a[i, j] = 0 if there is no edge between vertices i and j a[i, j] = 1 if there is an edge between vertices i and j Undirected graphs have both a[i, j] and a[j, i] = 1 if there is an edge between vertices i and j For weighted graphs, a[i,j] = weight of edge between vertices i and j

  18. A = (a ) ij 2 1 if (i, j)  E(G) a = 1 3 ij 0 otherwise 1 2 3 4 5 5 4 • 0 1 1 0 1 • 1 0 1 0 0 • 1 1 0 1 1 • 0 0 1 0 1 • 1 0 1 1 0 2 Space: (|V| ). Adjacency Matrix Preferred when the graph is small or dense.

  19. Graphs: Adjacency Matrix Example: 1 a d 2 4 Symmetric or un-symmetric? b c 3

  20. Graphs: Adjacency Matrix Example: 1 a d 2 4 b c 3

  21. Problem with Adjacency Matrix Space requirement is Q(N2) Problem: The array is very sparsely populated. For example if a directed graph has 4 vertices and 3 edges, the adjacency matrix has 16 cells only 3 of which are 1

  22. Adj 1 2 2 3 5 2 1 3 1 3 3 1 2 4 5 4 3 5 5 4 5 1 3 4 Adjacency Lists If G is directed, the total length of all the adjacency lists is | E |. If G is undirected, the total length is 2 | E |. Space requirement: (|V| + |E|). Preferable representation.

  23. Tradeoffs between Adjacency Matrices & Adjacency Lists Better for most Problems ? Lists

  24. Negative Cost Cycle A negative cost cycle is a cycle such that the sum of the costs of the edges is negative The more we cycle through a negative cost cycle, the lower the cost of the cycle becomes 2 3 4 5 1 • Cost of the path v1-v5 • No traversal of cycle: 6 • One traversal of cycle: 0 • Two traversals of cycle: -6 • Three traversals of cycle: -12 • ... 1 4 2 -12 5 • Negative cost cycles are not allowed when we traverse a graph to find the weighted shortest path

  25. Using a model to solve a complicated traffic light problem • GIVEN: A complex intersection. • OBJECTIVE: Traffic light with minimum phases. • SOLUTION: • Identify permitted turns, going straight is a “turn”. • Make group of permitted turns. • Make the smallest possible number of groups. • Assign each phase of the traffic light to a group.

  26. Using a model to solve a complicated traffic light problem D E C B A An intersection

  27. Using a model to solve a complicated traffic light problem Roads C and E are one way, others are two way. There are 13 permitted turns. Some turns such as AB (from A to B) and EC can be carried out simultaneously. Other like AD and EB cross each other and can not be carried out simultaneously. The traffic light should permit AB and EC simultaneously, but should not allow AD and EB.

  28. Using a model to solve a complicated traffic light problem AB & AC AD & EB D E C B A An intersection

  29. Using a model to solve a complicated traffic light problem • SOLUTION: • We model the problem using a structure called graph G(V,E). • A graph consists of a set of points called vertices, and lines • connecting the points, called edges. • Drawing a graph such that the vertices represent turns. • Edges between those turns that can NOT be performed • simultaneously.

  30. Using a model to solve a complicated traffic light problem AD AB AC ED BC BD BA DB DC EA EB EC DA Partial graph of incompatible turns D E C B A An intersection

  31. Using a model to solve a complicated traffic light problem Partial table of incompatible turns

  32. Using a model to solve a complicated traffic light problem • SOLUTION: • The graph can aid in solving our problem. • A coloring of a graph is an assignment of a color to each vertex • of the graph, so that no two vertices connected by an edge have • the same color. • Our problem is of coloring the graph of incompatible turns using • as few colors as possible.

  33. Using a model to solve a complicated traffic light problem • GIVEN: A complex intersection. • OBJECTIVE: Traffic light with minimum phases. • SOLUTION: • Identify permitted turns, going straight is a “turn”. • Make group of permitted turns. • Make the smallest possible number of groups. • Assign each phase of the traffic light to a group.

  34. Using a model to solve a complicated traffic light problem D E C B A An intersection

  35. Using a model to solve a complicated traffic light problem Roads C and E are one way, others are two way. There are 13 permitted turns. Some turns such as AB (from A to B) and EC can be carried out simultaneously. Other like AD and EB cross each other and can not be carried out simultaneously. The traffic light should permit AB and EC simultaneously, but should not allow AD and EB.

  36. Using a model to solve a complicated traffic light problem AB & AC AD & EB D E C B A An intersection

  37. Using a model to solve a complicated traffic light problem • SOLUTION: • We model the problem using a structure called graph G(V,E). • A graph consists of a set of points called vertices, and lines • connecting the points, called edges. • Drawing a graph such that the vertices represent turns. • Edges between those turns that can NOT be performed • simultaneously.

  38. Using a model to solve a complicated traffic light problem AD AB AC ED BC BD BA DB DC EA EB EC DA Partial graph of incompatible turns D E C B A An intersection

  39. Using a model to solve a complicated traffic light problem Partial table of incompatible turns

  40. Using a model to solve a complicated traffic light problem • SOLUTION: • The graph can aid in solving our problem. • A coloring of a graph is an assignment of a color to each vertex • of the graph, so that no two vertices connected by an edge have • the same color. • Our problem is of coloring the graph of incompatible turns using • as few colors as possible.

  41. Graphs Muhammad Amjad Iqbal

  42. Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems

  43. Graphs Searching

  44. Graph Searching Given: a graph G = (V, E), directed or undirected Goal: methodically explore every vertex and every edge Ultimately: build a tree on the graph Pick a vertex as the root Choose certain edges to produce a tree Note: might also build a forest if graph is not connected

  45. Breadth-First Search Given a G=(V,E) and distinguished source vertex s, BFS systematically explores the edges of G to “discover” every vertex reachable from s. Creates a BFS tree rooted at s that contains all such vertices. Expands the frontier between discovered and undiscovered vertices uniformly across the breadth of the frontier. The algorithm discovers all vertices at distance k from s before discovering any vertices at distance k+1

  46. will associate vertex “colors” to guide the algorithm White vertices have not been discovered All vertices start out white Grey vertices are discovered but not fully explored They may be adjacent to white vertices and represent the frontier between the discovered and the undiscovered. Black vertices are discovered and fully explored They are adjacent only to black and gray vertices Explore vertices by scanning adjacency list of grey vertices Breadth-First Search

  47. BFS(G, s) { for each vertex u V [G] - {s} do color[u] ← WHITE u->d ← ∞ u->p ← NIL Q = {s}; // Q is a queue initialize to s while (Q not empty) { u = Dequeue(Q); for each v  u->adj { if (v->color == WHITE) { v->color = GREY; v->d = u->d + 1; v->p = u; Enqueue(Q, v); } } u->color = BLACK; } } Breadth-First Search What does v->d represent? What does v->p represent?

  48. Breadth-First Search: Example r s t u         v w x y

  49. Breadth-First Search: Example r s t u  0       v w x y Q: s

  50. Breadth-First Search: Example r s t u 1 0    1   v w x y Q: w r

More Related