Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— 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 1

2 Directed Graph A directed graph, G = (V, E), is a data structure where: V is a set of vertices (aka nodes) E  V  V is a set of edges For any e i = (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., e i = (u, u) We use directed edges to represent asymmetrical relationships Examples: One-way streets, Causal relationship, Regulatory relationship, Web page links 2

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) Six degrees of separation Six degrees of Kevin Bacon 3

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 4

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 5

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) 6

7 Representing Graphs Directed, unweighted Adjacency matrixAdjacency List 7 0100 0110 1001 1000 abcdabcd b cb a a a b c d abcdabcd source target d

8 Representing Graphs Directed, weighted Adjacency matrixAdjacency List 8 0200 0030 008 9000 abcdabcd b,2 c,3 a,-1 a,9 a b c d abcdabcd source target d,8

9 Representing Graphs Undirected, unweighted Adjacency matrixAdjacency List 9 abcdabcd a b c d abcdabcd source target

10 Representing Graphs Undirected, weighted Adjacency matrixAdjacency List 10 abcdabcd a b c d abcdabcd source 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(?) 11

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(?) 12

13 Paths and Cycles Let G = (V,E) be a graph, a path is a sequence of m vertices v 1, v 2, v 3, …, v m-1, v m such that (v i, v i+1 )  E for 1  i  m-1 v 1 is the source vertex of the path v m is the target vertex of the path The length of the path is m-1 A cycle is a path where v 1 == v m 13

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? 14

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) 15

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, v i ) If v i has not yet been visited Include v i to a list of nodes reachable from u Recursively perform DFS on v i Note: We have to keep track of visited nodes because there might be cycles in the graph 16

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 17

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 18

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? 19


Download ppt "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."

Similar presentations


Ads by Google