1 / 19

Graph

Graph. A graph , G = (V, E) , is a data structure where: V is a set of vertices (aka nodes) E is a set of edges We use graphs to represent relationships among objects (relationship = edge, object = node) Examples Physical Networks: Communication, Information, Transportation, Mazes

garron
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 • A graph, G = (V, E), is a data structure where: • V is a set of vertices (aka nodes) • E is a set of edges • We use graphs to represent relationships among objects(relationship = edge, object = node) • Examples • Physical Networks: • Communication, Information, Transportation, Mazes • Social Networks: • Facebook, LinkedIn, Google+ • Dependency Networks: • Flow charts, State machines, Bayes Nets, Markov Random Fields, Regulatory Networks

  2. Directed Graph • A directed graph, G = (V, E), is a data structure where: • V is a set of vertices (aka nodes) • E  V  Vis a set of edges • For any ei = (u, v)  E “คู่อันดับ – ordered pairs” • u  V is the source node • v  V is the target node (aka destination node) • Note: In some graphs we allow self-edges • i.e., ei = (u, u) • We use directed edges to represent asymmetrical relationships • Examples: One-way streets, Causal relationship, Regulatory relationship, Web page links

  3. Undirected Graph • An undirected graph, G = (V, E), is a data structure where: • V is a set of vertices (aka nodes) • E = { {u,v} | u, v  V }is a set of edges • Here E is a set of คู่ไม่อันดับ – unordered pairs • We use undirected edges to represent symmetrical relationships • Examples: • Two-way streets • Network traffic • Mazes • Six degrees of separation (and Six degrees of Kevin Bacon)

  4. Weighted Graph • An weighted graph, G = (V, E, W), is a data structure where: • V is a set of vertices (aka nodes) • E is a set of edges (directed or undirected) • W: E  (i.e., a function from E to ) • Examples: Distances on a map

  5. Properties of graphs • |V| = number of nodes • |E| = number of edges • If we allow self-edges • In a directed graph, |E|  |V|2 • In an undirected graph, |E|  |V|*(|V|-1)/2 + |V| • A sparse graph is one where |E| = O(|V|) or less • Most ‘real world’ graphs are sparse • Transportation networks, www, mazes, Facebook • A dense graph is one where |E| = O(|V|2) or more

  6. Properties of graphs • The degree of a node is the number of edges coming in or out of that node • In undirected graphs • Degree(u) = | { v  V : {u,v}  E } | • Thus, Degree(u) is the number of “neighbors” of u • In directed graphs • In-degree(u) = | { v  V : (v,u)  E } | • Out-degree(u) = | { v  V : (u,v)  E } | • Degree(u) = In-degree(u) + Out-degree(u)

  7. Representing Graphs • Directed, unweighted Adjacency matrix Adjacency List a b c d b source b c a d a b c d a target

  8. Representing Graphs • Directed, weighted Adjacency matrix Adjacency List a b c d b,2 source c,3 a,-1 d,8 a b c d a,9 target

  9. Representing Graphs • Undirected, unweighted Adjacency matrix Adjacency List a b c d source a b c d target

  10. Representing Graphs • Undirected, weighted Adjacency matrix Adjacency List a b c d source a b c d target

  11. Mini-Quiz • Undirected or Directed Graphs • Space Complexity • Adjacency Matrix Representation: O(?) • Adjacency List Representation: O(?) • Which is the better choice if the graph is sparse? • How expensive is it to check whether (u,v)  E ? • Adjacency Matrix Representation: O(?) • Adjacency List Representation: O(?) • Undirected Graphs • How expensive is it to compute the degree of a node? • Adjacency Matrix Representation: O(?) • Adjacency List Representation: O(?)

  12. Mini-Quiz • Directed Graphs • How expensive is it to compute the out-degree of a node? • Adjacency Matrix Representation: O(?) • Adjacency List Representation: O(?) • How expensive is it to compute the in-degree of a node? • Adjacency Matrix Representation: O(?) • Adjacency List Representation: O(?)

  13. Paths and Cycles • Let G = (V,E) be a graph, a path is a sequence of m vertices v1, v2, v3, …, vm-1, vmsuch that (vi, vi+1)  E for 1im-1 • v1 is the source vertex of the path • vm is the target vertex of the path • The length of the path is m-1 • A cycle is a path where v1 == vm

  14. Simple Paths and Simple Cycles • A path is simple if there is at most one copy of each vertex in the path • What is the maximum length of a simple path in a graph with |V| nodes? • A cycle is simple if there is at most one copy of each vertex, except the source/target, in the cycle • What is the maximum length of a simple cycle in a graph with |V| nodes?

  15. Reachability • One of the most common tasks for computing over graphs is to find the subset of nodes reachable from some particular node u • This is called “reachability problem” • Special case: determine whether there is a path from some particular node u to some particular node t • There are two fundamental algorithms for answering this question • Depth-first search (DFS) • Breadth-first search (BFS)

  16. Depth-First Search (DFS) • DFS Algorithm on a node u, • Make sure to keep track of nodes that has been visited • For each edge (u, vi) • If vi has not yet been visited • Include vi to a list of nodes reachable from u • Recursively perform DFS on vi • Note: We have to keep track of visited nodes because there might be cycles in the graph

  17. Breadth-First Search (BFS) • BFS Algorithm on a node u, • Make sure to keep track of nodes that has been visited • Initialization: • Enqueue u to a queue • Add u to a list of nodes reachable from u • While the queue isn’t empty • Dequeue node v from front of queue • For each edge (v,w) • If w hasn’t been visited • Enqueue w to a queue • Add w to a list of nodes reachable from u

  18. Depth-First Search (BFS) … again • DFS Algorithm on a node u, • Make sure to keep track of nodes that has been visited • Initialization: • Push u to a stack • Add u to a list of nodes reachable from u • While the stack isn’t empty • Pop node v from the stack • For each edge (v,w) • If w hasn’t been visited • Push w to a stack • Add w to a list of nodes reachable from u

  19. Analyzing BFS, DFS algorithms • What’s the total cost of a DFS/ BFS? • Hints • How many times do we process each node in a DFS/BFS? • How many times do we cross each edge in a DFS/BFS?

More Related