1 / 54

What is a graph?

What is a graph?. A data structure that consists of a set of nodes ( vertices ) and a set of edges that relate the nodes to each other The set of edges describes relationships among the vertices. Visit for more Learning Resources. Formal definition of graphs.

sorensena
Download Presentation

What is a 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. What is a graph? • A data structure that consists of a set of nodes (vertices) and a set of edges that relate the nodes to each other • The set of edges describes relationships among the vertices Visit for more Learning Resources

  2. Formal definition of graphs • A graph G is defined as follows: G=(V,E) V(G): a finite, nonempty set of vertices E(G): a set of edges (pairs of vertices)

  3. head tail The Graph ADT (2/13) • Definitions • A graph G consists of two sets • a finite, nonempty set of vertices V(G) • a finite, possible empty set of edges E(G) • G(V,E) represents a graph • An undirected graph is one in which the pair of vertices in an edge is unordered, (v0, v1) = (v1,v0) • A directed graph is one in which each edge is a directed pair of vertices, <v0, v1> != <v1, v0>

  4. 0 0 0 0 1 1 2 1 2 1 2 3 3 6 5 2 4 3 G1 G2 G3 The Graph ADT (3/13) • Examples for Graph • complete undirected graph: n(n-1)/2 edges • complete directed graph: n(n-1) edges complete graph incomplete graph V(G1)={0,1,2,3} E(G1)={(0,1),(0,2),(0,3),(1,2),(1,3),(2,3)} V(G2)={0,1,2,3,4,5,6} E(G2)={(0,1),(0,2),(1,3),(1,4),(2,5),(2,6)} V(G3)={0,1,2} E(G3)={<0,1>,<1,0>,<1,2>}

  5. The Graph ADT (4/13) • Restrictions on graphs • A graph may not have an edge from a vertex, i, back to itself. Such edges are known as self loops • A graph may not have multiple occurrences of the same edge. If we remove this restriction, we obtain a data referred to as a multigraph feedback loops multigraph

  6. V0 V1 The Graph ADT (5/13) • Adjacent and Incident • If (v0, v1) is an edge in an undirected graph, • v0 and v1 are adjacent • The edge (v0, v1) is incident on vertices v0 and v1 • If <v0, v1> is an edge in a directed graph • v0 is adjacent to v1, and v1 is adjacent from v0 • The edge <v0, v1> is incident on v0 and v1 V0 V1

  7. 0 1 2 3 G1 0 1 2 G3 The Graph ADT (6/13) • A subgraph of G is a graph G’ such that V(G’)  V(G) and E(G’)  E(G).

  8. 0 0 0 1 2 1 2 1 2 3 3 3 The Graph ADT (7/13) • Path • A path from vertex vp to vertex vq in a graph G, is a sequence of vertices, vp, vi1, vi2, ..., vin, vq, such that (vp, vi1), (vi1, vi2), ..., (vin, vq) are edges in an undirected graph. • A path such as (0, 2), (2, 1), (1, 3) is also written as 0, 2, 1, 3 • The length of a path is the number of edges on it

  9. 0 1 2 3 The Graph ADT (8/13) • Simple path and cycle • simple path (simple directed path): a path in which all vertices, except possibly the first and the last, are distinct. • A cycle is a simple path in which the first and the last vertices are the same.

  10. The Graph ADT (9/13) • Connected graph • In an undirected graph G, two vertices, v0 and v1, are connected if there is a path in G from v0 to v1 • An undirected graph is connected if, for every pair of distinct vertices vi, vj, there is a path from vi to vj • Connected component • A connected component of an undirected graph is a maximal connected subgraph. • A tree is a graph that is connected and acyclic (i.e, has no cycle).

  11. 0 0 1 2 1 G3 The Graph ADT (10/13) • Strongly Connected Component • A directed graph is strongly connected if there is a directed path from vi to vj and also from vj to vi • A strongly connected component is a maximal subgraph that is strongly connected strongly connected component (maximal strongly connected subgraph) 2 not strongly connected

  12. The Graph ADT (11/13) • Degree • The degree of a vertex is the number of edges incident to that vertex. • For directed graph • in-degree (v) : the number of edges that have v as the head • out-degree (v) : the number of edges that have v as the tail • If di is the degree of a vertex i in a graph G with n vertices and e edges, the number of edges is

  13. Weighted Graphs Each edge has an associated weight or cost. 20 Clinton Mukilteo 30 Kingston Edmonds 35 Bainbridge Seattle 60 Bremerton There may be more information in the graph as well.

  14. Paths and Cycles A path is a list of vertices {v1, v2, …, vn} such that (vi, vi+1)  E for all 0  i < n. A cycle is a path that begins and ends at the same node. Chicago Seattle Salt Lake City San Francisco Dallas p = {Seattle, Salt Lake City, Chicago, Dallas, San Francisco, Seattle}

  15. Path Length and Cost Path length: the number of edges in the path Path cost: the sum of the costs of each edge Chicago 3.5 Seattle 2 2 Salt Lake City 2 2.5 2.5 2.5 3 San Francisco Dallas length(p) = 5 cost(p) = 11.5

  16. Trees as Graphs • Every tree is a graph with some restrictions: • the tree is directed • there are no cycles (directed or undirected) • there is a directed path from the root to every node A B C D E F G H BAD! I J

  17. Directed Acyclic Graphs (DAGs) DAGs are directed graphs with no cycles. main() mult() if program call graph is a DAG, then all procedure calls can be in-lined add() read() access() Trees  DAGs  Graphs

  18. Directed vs. undirected graphs • When the edges in a graph have no direction, the graph is called undirected

  19. Directed vs. undirected graphs (cont.) • When the edges in a graph have a direction, the graph is called directed (or digraph) Warning: if the graph is directed, the order of the vertices in each edge is important !! E(Graph2) = {(1,3) (3,1) (5,9) (9,11) (5,7)

  20. Directed vs. Undirected Graphs • In directed graphs, edges have a specific direction: • In undirected graphs, they don’t (edges are two-way): • Vertices u and v are adjacent if (u, v)  E Han Luke Leia Han Luke Leia

  21. Trees vs graphs • Trees are special cases of graphs!!

  22. Graph terminology • Adjacent nodes: two nodes are adjacent if they are connected by an edge • Path: a sequence of vertices that connect two nodes in a graph • Complete graph: a graph in which every vertex is directly connected to every other vertex 5 is adjacent to 7 7 is adjacent from 5

  23. Complete Graph • A complete graph is a graph that has the maximum number of edges • for undirected graph with n vertices, the maximum number of edges is n(n-1)/2 • for directed graph with n vertices, the maximum number of edges is n(n-1) • example: G1 is a complete graph CHAPTER 6

  24. Examples for Graph 0 0 0 1 2 1 2 1 3 3 6 5 4 G1 2 G2 complete graph incomplete graph G3 V(G1)={0,1,2,3} E(G1)={(0,1),(0,2),(0,3),(1,2),(1,3),(2,3)} V(G2)={0,1,2,3,4,5,6} E(G2)={(0,1),(0,2),(1,3),(1,4),(2,5),(2,6)} V(G3)={0,1,2} E(G3)={<0,1>,<1,0>,<1,2>} complete undirected graph: n(n-1)/2 edges complete directed graph: n(n-1) edges CHAPTER 6

  25. Adjacent nodes & Incident nnodes • If (v0, v1) is an edge in an undirected graph, • v0 and v1 are adjacent nodes • The edge (v0, v1) is incident on vertices v0 and v1 • If <v0, v1> is an edge in a directed graph • v0 is adjacent to v1, and v1 is adjacent from v0 • The edge <v0, v1> is incident on v0 and v1 CHAPTER 6

  26. *Figure 6.3:Example of a graph with feedback loops and a multigraph (p.260) 0 0 2 1 3 1 2 multigraph: multiple occurrences of the same edge self edge (b) (a) Figure 6.3 CHAPTER 6

  27. Subgraph and Path • A subgraph of G is a graph G’ such that V(G’) is a subset of V(G) and E(G’) is a subset of E(G) • A path from vertex vp to vertex vq in a graph G, is a sequence of vertices, vp, vi1, vi2, ..., vin, vq, such that (vp, vi1), (vi1, vi2), ..., (vin, vq) are edges in an undirected graph • The length of a path is the number of edges on it CHAPTER 6

  28. Figure 6.4: subgraphs of G1 and G3 (p.261) 0 0 1 2 0 1 2 3 1 2 3 0 1 2 3 (i) (ii) (iii) (iv) (a) Some of the subgraph of G1 G1 G3

  29. 0 0 0 0 1 1 1 2 2 0 1 (i) (ii) (iii) (iv) (b) Some of the subgraph of G3 2 CHAPTER 6

  30. Connected Component • A connected component of an undirected graph is a maximal connected subgraph. • A tree is a graph that is connected and acyclic. • A directed graph is strongly connected if there is a directed path from vi to vj and also from vj to vi. • A strongly connected component is a maximal subgraph that is strongly connected. CHAPTER 6

  31. *Figure 6.5: A graph with two connected components (p.262) 4 H2 0 H1 5 1 2 6 3 7 G4 (not connected) connected component (maximal connected subgraph) CHAPTER 6

  32. *Figure 6.6: Strongly connected components of G3 (p.262) strongly connected component (maximal strongly connected subgraph) not strongly connected 0 2 0 1 1 2 G3 CHAPTER 6

  33. Degree • The degree of a vertex is the number of edges incident to that vertex • For directed graph, • the in-degree of a vertex v is the number of edgesthat have v as the head • the out-degree of a vertex v is the number of edgesthat have v as the tail • if di is the degree of a vertex i in a graph G with n vertices and e edges, the number of edges is CHAPTER 6

  34. undirected graph degree 0 3 0 2 1 2 1 2 3 3 3 3 3 6 5 4 3 3 1 G1 1 1 G2 1 0 in:1, out: 1 directed graph in-degree out-degree in: 1, out: 2 1 in: 1, out: 0 2 G3 CHAPTER 6

  35. Graph Representations • Adjacency Matrix • Adjacency Lists • Adjacency Multilists CHAPTER 6

  36. Graph Representations (1/13) • Adjacency Matrix • Adjacency Lists • Adjacency Multilists

  37. Graph implementation • Array-based implementation • A 1D array is used to represent the vertices • A 2D array (adjacency matrix) is used to represent the edges

  38. Graph implementation (cont.) • Linked-list implementation • A 1D array is used to represent the vertices • A list is used for each vertex v which contains the vertices which are adjacent from v (adjacency list)

  39. Adjacency Matrix • Let G=(V,E) be a graph with n vertices. • The adjacency matrix of G is a two-dimensional n by n array, say adj_mat • If the edge (vi, vj) is in E(G), adj_mat[i][j]=1 • If there is no such edge in E(G), adj_mat[i][j]=0 • The adjacency matrix for an undirected graph is symmetric; the adjacency matrix for a digraph need not be symmetric

  40. Examples for Adjacency Matrix 4 0 5 1 2 6 3 7 0 0 1 2 3 1 2 G1 G2 symmetric undirected: n2/2 directed: n2 G4

  41. 0 4 0 5 1 2 6 3 7 1 2 3 0 1 2 3 1 1 2 3 2 0 1 2 3 4 5 6 7 0 2 3 0 3 0 1 3 0 3 1 2 1 2 0 5 G1 0 6 4 0 1 2 1 5 7 1 0 2 6 G4 G3 2 An undirected graph with n vertices and e edges ==> n head nodes and 2e list nodes

  42. Some Graph Operations • TraversalGiven G=(V,E) and vertex v, find all wV, such that w connects v. • Depth First Search (DFS)preorder tree traversal • Breadth First Search (BFS)level order tree traversal • Connected Components • Spanning Trees

  43. Recall: Tree Traversals a e b c d h i j f g k l a b f g k c d h i l j e

  44. Depth-First Search • Pre/Post/In – order traversals are examples of depth-first search • Nodes are visited deeply on the left-most branches before any nodes are visited on the right-most branches • Visiting the right branches deeply before the left would still be depth-first! Crucial idea is “go deep first!” • Difference in pre/post/in-order is how some computation (e.g. printing) is done at current node relative to the recursive calls • In DFS the nodes “being worked on” are kept on a stack

  45. Iterative Version DFSPre-order Traversal Push root on a Stack Repeat until Stack is empty: Pop a node Process it Push it’s children on the Stack

  46. a e b c d h i j f g k l Level-Order Tree Traversal • Consider task of traversing tree level by level fromtop to bottom (alphabetic order) • Is this also DFS?

  47. Breadth-First Search • No! Level-order traversal is an example of Breadth-First Search • BFS characteristics • Nodes being worked on maintained in a FIFO Queue, not a stack • Iterative style procedures often easier to design than recursive procedures Put root in a Queue Repeat until Queue is empty: Dequeue a node Process it Add it’s children to queue

  48. a e b c d h i j f g k l QUEUE a b c d e c d e f g d e f g e f g h i j f g h i j g h i j h i j k i j k j k l k l l

  49. Graph Traversals • Depth first search and breadth first search also work for arbitrary (directed or undirected) graphs • Must mark visited vertices so you do not go into an infinite loop! • Either can be used to determine connectivity: • Is there a path between two given vertices? • Is the graph (weakly) connected? • Important difference: Breadth-first search always finds a shortest path from the start vertex to any other (for unweighted graphs) • Depth first search may not!

More Related