Graph: representation and traversal CISC4080, Computer Algorithms CIS, Fordham Univ. Instructor: X. Zhang
Outline Graph Definition • Graph Representation • Path, Cycle, Tree, Connectivity • Graph Traversal Algorithms • Breath first search/traversal • Depth first search/traversal • … • 2
Graph Graph: a set of nodes (vertices) with edges (links) • between them, G=(V,E), where V is set of vertices, E: set of edges • model binary relation (i.e., whether two elements/ people/cities/courses are related or not) e.g., WWW: nodes are webpages, edges are • hyperlinks, e.g.,<a href=“ www.wikipedia.com ”>wiki</a> 3
Graph: application • Binary relation could be symmetric or asymmetric • Symmetric relation: always go both way – e.g., for any two people A and B, if A is B’s facebook friend, then B is also A’s facebook friend. – no need to draw an arrow ==> Undirected graph • Asymmetric relation: not always go both way – e.g., my web page linked to cnn.com, but not vice versa – use an arrow to show “direction” of the relation 4
Undirected Graphs • Undirected Graph: no arrow on edges • each edge can be represented as a set of two nodes • degree of a node: the # of edges sticking out of the node V={1,2,3,4} E={ {1,2), {1,3}, {2,4}, {2,3}} 1 2 degree of node 1: 3 4 degree of node 4: total degree of all nodes: 8 total number of edges: 4 relation between above two quantities: total degree=2|E|? 5
Directed Graphs • Directed Graphs: each edge has an arrow showing the direction of the binary relation • e.g., 1 is related to 2, but 2 is not related to 1 • each edge is an ordered pair • Degree of a node • in-degree: number of edges pointing to a node • out-degree: number of edges coming out from a node V={1,2,3,4} 1 2 E={ (1,2), (2,2), (3,1), (2,3), (2,4),(3,4), (4,3)} in-degree of node 2: 2 3 4 out-degree of node 2:3 6
Outline • Graph Definition • Graph Representation • Path, Cycle, Tree, Connectivity • Graph Traversal Algorithms • Breath first search/traversal 7
Graph Representation • How to store a graph, G=(V,E) in computer program? • V: the set of nodes, can be stored in a vector or an array of nodes • E: the set of edges (adjacency relation between nodes) can be stored as • adjacency list, or • adjacency matrix 8
Adjacency list, G=(V,E) • For each node u, maintain a list Adj[u] (adjacency list) which stores all vertices v such that there is an edge from u to v • Organize adjacency lists into an array • Can be used for both directed and undirected graphs Adj 1 2 5 / 1 2 2 4 / 1 3 5 3 3 2 4 4 3 / 2 5 5 4 5 2 4 1 Undirected graph 9
Properties of Adjacency List Representation • Memory required – Θ (|V|+|E|) • Advantage: save memory when graph is sparse, i.e., |E| << |V| 2 • Disadvantage • to determine whether there is an edge between node u and v, need to traverse adj[u], with time O(out-degree(u)) • Time to list all vertices adjacent to u: Θ (out-degree(u)) 10
Adjacency matrix representation • Assume nodes are numbered 1, 2, … n, n=|V| • Represent E using a matrix A nxn a ij = 1 if (i, j) belongs to E, i.e., if there is edge (i,j) 0 otherwise a ij is the i-th rown, j-th column in the matrix A 1 2 3 4 5 For undirected graphs, 0 1 0 0 1 1 matrix A is symmetric: 1 2 1 0 1 1 1 2 a ij = a ji for any i,j A = A T 3 0 1 0 1 0 3 5 4 0 1 1 0 1 4 1 1 0 1 0 Undirected graph 5 11
Properties of Adjacency Matrix • Memory required – Θ (|V| 2 ), independent of number of edges in G • Preferred when – graph is dense: |E| is close to |V| 2 – need to quickly determine if there is an edge between two vertices • Time to list all vertices adjacent to u: – Θ (n) • Time to determine if (u, v) belongs to E: – Θ (1) 12
Weighted Graphs • Weighted graphs: in which each edge has an associated weight w(u, v) (a real number) – you can think of w as a function that maps each edge to a real value (indicating cost of traversing the edge, e.g.) • w: E -> R, weight function • Storing weights of a graph – Adjacency list: • Store w(u,v) along with vertex v in u’s adjacency list – Adjacency matrix: • Store w(u, v) at location (u, v) in the matrix 13
Outline Graph Definition • Graph Representation • Path, Cycle, Tree, Connectivity • Graph Traversal Algorithms • BFS • 14
Paths • A Path in a graph G=(V,E) is a sequence of nodes v 1 ,v 2 , …,v k , where each and every consecutive pair v i-1 , v i is joined by an edge in E, i.e., (v i-1 , v i ) is an edge • Length of a path: number of edges traversed by the path G 2 1 2 1 2 G 1 Are these paths? 3 4 1,2,3: path, l=2 3 4 is1,3,2 a path in G 1 ? 1,2,3,1,2,4: path, 5 1,2,3,1: yes Is1,2,3,4 a path in G1; no, .. 1,3,2: not a path 15
Simple Path A path is simple if all nodes in the path are distinct. • • i.e., we don’t revisit a node… • A cycle is a path v 1 ,v 2 ,…,v k where v 1 =v k , k>2, and the first k-1 nodes are all distinct • Which one is simple path? which one is cycle? 1 2 G 2 1,2,3: simple path G 1 2 1 1,2,3,1,2,4: not simple 1,2,3,1: not simple, cycle. 3 4 1,3,2 3 4 1,3,2 1,2,3,4 1, 2, 3, 1, 2 16
Shortest (hop) Paths • There are often times multiple paths from u to v. All paths from 1 to 4? 1 2 1) 1,3, 2, 4: length of path is 2) 1, 2, 4 <— shortest(-hop) path from 1 to 4 3 4 We say node 4 is 2-hop away from Node 1. 2 is the predecessor node for node 4 3) 1, 3, 2, 1, 2, 4 …. There are infinite! • Shortest path from u to v is a path from u to v, with distance that is smaller than or equal to all other paths from u to v • Shortest path from u to v is always a simple path. 17
Exercise • For G=(V,E), where V={1,2,3,4,5,6}, with following adjacency matrix: • Is it directed? undirected? Why? • Is 2,3,4,5,6 a path? Why? Directed or undirected=> is the matrix symmetric along main diagonal line? Undirected graph Is a_23 a_34 a_45 a_56 18
Exercise • A graph is connected if and only if • for any two nodes u, v, there is a path connecting u to v. • For G=(V,E), V={1,2,3,4,5,6}, with following adjacency matrix: • First draw graph, then decide whether it is connected. 19
Outline Graph Definition • Graph Representation • Path, Cycle, Connectivity • Graph Traversal Algorithms • basis of other algorithms • 20
Traversing a Graph Graph traversal: starting from one node • initially, visit other nodes by following edges of graph in a systematic way • Two basic graph traversal algorithms: – Breadth-first search – Depth-first search They differ in the order in which they explore – unvisited edges of the graph 21
Breadth-First Search (BFS) Given a graph G = (V, E) , a source node s from V • Follow edges of G to “discover” every node reachable from s • first follow edges from s to discover all nodes that are one-hop • away from s from these one-hop away nodes, discover nodes that are two-hop • away from s, …, …, from k-hop away nodes, discover nodes that are k+1-hop away … • until all reachable nodes have been visited • Example: starting from source node 1, list nodes that are one-hop, two- • hop away from node 1? S=1 1 2 3 5 4 22
Nodes Coloring: avoid revisit • When exploring node 2’s neighboring nodes, we re- discover node 1. • we will be stuck in a loop • Solution: use color to represent node state – White: not yet discovered – Gray: discovered, but not explored yet (i.e., we haven’t explored its neighboring nodes yet) – Black: discovered, and explored 2 1 source 3 Node 1 would be Black when we 5 4 explore node 2 23
FIFO Queue: all gray nodes • How to maintain all grey nodes, i.e., discovered and yet to be explored? • Observation: in BFS, Discover nodes with smaller hop count first, and explore nodes with smaller hop count first • Solution: enqueue nodes when they are discovered, and dequeue them one by one to explore source Example, FIFO queue Q First, source node is discovered, Q=1 • Remove 1 from Q, explores its neighbors, • Q=3, 2 • Remove 3 from Q, explores its neighbors, Q=2, 5, 6 Q = 5, 6, 4 24
Breadth First Traversal: how to 1. Initially, all nodes are white 2. source node s is discovered first, color it gray, and insert it into a FIFO queue, Q 3. Take next node u from queue Q, • follow edges coming out from u to discover all its adjacent white nodes, color them gray, insert to Q • color u black 4. Go back to 3, until Q is empty start step 3 after step 3 source step 1 step 2 1 2 1 2 1 2 1 2 3 3 3 3 5 4 5 4 5 4 5 4 Q: 5 25 Q: 1 Q: 2, 5
Recommend
More recommend