Graph: representation and traversal CISC4080, Computer Algorithms - - PowerPoint PPT Presentation
Graph: representation and traversal CISC4080, Computer Algorithms - - PowerPoint PPT Presentation
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
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
- f 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
- f the node
1 2 3 4
5
V={1,2,3,4} E={ {1,2), {1,3}, {2,4}, {2,3}} degree of node 1: degree of node 4: total degree of all nodes: 8 total number of edges: 4 relation between above two quantities: total degree=2|E|?
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
1 2 3 4
6
V={1,2,3,4} E={ (1,2), (2,2), (3,1), (2,3), (2,4),(3,4), (4,3)} in-degree of node 2: 2
- ut-degree of node 2:3
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
1 2 5 4 3
2 5 / 1 5 3 4 /
1 2 3 4 5
2 4 2 5 3 / 4 1 2
Undirected graph
9 Adj
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 Anxn
aij = 1 if (i, j) belongs to E, i.e., if there is edge (i,j) 0 otherwise
1 2 5 4 3 Undirected graph 1 2 3 4 5 1 2 3 4 5 1 1 1 1 1 1 1 1 1 1 1 1 1 1
For undirected graphs, matrix A is symmetric: aij = aji for any i,j A = AT
11
aij is the i-th rown, j-th column in the matrix A
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 v1,v2,
…,vk , where each and every consecutive pair vi-1, vi is joined by an edge in E, i.e., (vi-1, vi) is an edge
- Length of a path: number of edges traversed by the path
15
1 2 3 4 1 2 3 4
is1,3,2 a path in G1? Is1,2,3,4 a path in G1; no, ..
Are these paths? 1,2,3: path, l=2 1,2,3,1,2,4: path, 5 1,2,3,1: yes 1,3,2: not a path
G1 G2
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 v1,v2,…,vk where v1=vk, k>2, and the
first k-1 nodes are all distinct
- Which one is simple path? which one is cycle?
16
1 2 3 4 1 2 3 4
1,3,2 1,2,3,4 1, 2, 3, 1, 2
1,2,3: simple path 1,2,3,1,2,4: not simple 1,2,3,1: not simple, cycle. 1,3,2
G1 G2
Shortest (hop) Paths
- There are often times multiple paths from u to v.
- 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
1 2 3 4 All paths from 1 to 4? 1) 1,3, 2, 4: length of path is 2) 1, 2, 4 <— shortest(-hop) path from 1 to 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!
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?
18 Directed or undirected=> is the matrix symmetric along main diagonal line? Undirected graph Is a_23 a_34 a_45 a_56
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
- 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
Traversing a 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?
22
1 2 5 4 3
S=1
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 Node 1 would be Black when we explore node 2
source 1 2 5 4 3
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 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
source
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
1 2 5 4 3 1 2 5 4 3 source 1 2 5 4 3
25
step 1 step 2 after step 3 1
2
5 4 3 start step 3
Q: 1 Q: 2, 5 Q: 5
- 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,
- for each of these node, v, insert it to Q
- color[v] = gray
- set d[v]=d[u]+1
- set pred[v]=u
- color u black
- 4. Go back to 3, until Q is empty
– d[v]: length of shortest path from s to v – pred[v]: predecessor node in shortest path from s to v
BFS output
26
1 2 5 4 3
S d[2]=1 pred[2]=1 d[5]=1 pred[5]=1 d[3]=2 pred[3]=2 d[4]=2 pred[4]=2 d[1]=0 pred[1]=NULL
BFS: Implementation Detail
- Three maps/dictionaries:
- color[u] – color of vertex u in V
- pred[u] – predecessor of u
– If u = s (root) or node u has not yet been discovered then pred[u] = NIL
- d[u] – distance from source s to vertex u
1 2 5 4 3 d=1 pred =1 d=1 pred =1 d=2 pred=5 d=2 pred =2 source
27
BFS(V, E, s): tracing
1. BFS( V, E, s) 2. { 3. for each u in V - {s} 4. color[u] = WHITE 5. d[u] ← ∞ 6. pred[u] = NIL 5. color[s] = GRAY, 6. d[s] ← 0, pred[s] = NIL 8. Q = empty 9. ENQUEUE(Q, s)
28
10. while Q not empty 11. u ← DEQUEUE(Q) 12. for each v in Adj[u] 13. if color[v] = WHITE 14. color[v] = GRAY 15. d[v] ← d[u] + 1 16. pred[v] = u 17. ENQUEUE(Q, v) 18. color[u] = BLACK 19. }
r s t u v w x y
BFS(V, E, s)
1. BFS( V, E, s) 2. { 3. for each u in V - {s} 4. color[u] = WHITE 5. d[u] ← ∞ 6. pred[u] = NIL 5. color[s] = GRAY, 6. d[s] ← 0, pred[s] = NIL 8. Q = empty 9. ENQUEUE(Q, s) 29
10. while Q not empty 11. u ← DEQUEUE(Q) 12. for each v in Adj[u] 13. if color[v] = WHITE 14. color[v] = GRAY 15. d[v] ← d[u] + 1 16. pred[v] = u 17. ENQUEUE(Q, v) 18. color[u] = BLACK 19. }
r s t u v w x y
Suppose BFS(V, E, r) is called , i.e., src node is r label node (d, pred)
a b c
Analysis of BFS
1. for each u ∈ V - {s} 2. do color[u] ← WHITE 3. d[u] ← ∞ 4. pred[u] = NIL 5. color[s] ← GRAY 6. d[s] ← 0 7. pred[s] = NIL 8. Q ← ∅ 9. ENQUEUE(Q, s) O(|V|) Θ(1)
30
Analysis of BFS
Scan Adj[u] for all nodes u in graph
- Each u is processed only once
- Sum of lengths of all adjacency lists = Θ(|
E|)
- So: while loop has # steps that is O(|E|)
Total running time for BFS: O(|V| + |E|)
31
10. while Q not empty 11. u ← DEQUEUE(Q) 12. for each v in Adj[u] 13. if color[v] = WHITE 14. then color[v] = GRAY 15. d[v] ← d[u] + 1 16. pred[v] = u 17. ENQUEUE(Q, v) 18. color[u] = BLACK
Property of Breadth-First Traversal
- Node u is reachable from s: there is a path from s to u
- A BFS from source node s discovers all nodes, u, that
are reachable from s
- node 6 below won’t be discovered
- some nodes might stay white after BFS.
32
1 2 5 4 3
S=1 6
Property of Breadth-First Traversal
- If u is k-hop away from s, then after BFS(G,s)
- d[u]=k,
- pred[u] is set to pred. node in shortest path s, … ,?, u
- Why?
- reason using math. induction on k
- true for k=1 (e.g., node 2, 5).
- If true for all nodes k-hop away, then also true for K+1-
hop nodes
33
1 2 5 4 3
S=1 6
Recall: u is k-hop away from s: length of shortest hop path from s to u is k
Shortest Path
- BFS discovers shortest paths for all nodes that are
reachable from s!
- pred[u] is a back-pointer storing previous hop
- To get shortest path to u: follows pred[u] to its predecessor v,
and then use pred[v] to find its predecessor’s predecessor, and so on and on, until we get back to s
- u, pred[u], pred[pred[u]], … s
- What’s shortest path from s to 4 based upon BFS output?
34
1 2 5 4 3
S d[2]=1 pred[2]=1 d[5]=1 pred[5]=1 d[3]=2 pred[3]=2 d[4]=2 pred[4]=2
BFS Tree
- BFS tree G’=(V’,E’) is a subgraph of G=(V,E)
- V’ is a subset of V, including all nodes that are reachable from
s
- E’ is a subset of E, contains by all edges from predecessor
node to node
- e.g., (pred[2], 2), (pred[3], 3), (pred[4], 4), (pred[5],5), i.e.,
- (1,2), ( 1,5),(2,4), (2,3) colored in red below
35
1 2 5 4 3
S d[2]=1 pred[2]=1 d[5]=1 pred[5]=1 d[3]=2 pred[3]=2 d[4]=2 pred[4]=2
BFS: Example
- Suppose we perform BFS on following graph, with s=1
- What’s the output? Label each node with (d[v], pred[v])
– d[v]: distance (shortest hop) from s to v, for all v – pred[v]: predecessor node in shortest path from s to v,
36
Graph and Puzzle
- Many puzzles can be modeled as
graph:
- nodes:
- edges: legal moves
- Such graph representation (state
space of the puzzle)
- allows one to use graph
algorithms (traversal, A* search) to solve puzzle
- Exercise:
- how many nodes is “initial
State” node connected with?
- Is the graph directed?
37
Exercise
- First two glasses (one with a volume of 3 oz., one
with a volume of 5 oz.)are empty. The third glass (8 oz.) is full of water. How to get at least one glass to hold 4 oz. of water by pouring water from
- ne glass to another (assuming no spillage).
- What’s best way (with minimal pouring)?
38
Water Pouring Puzzle
39
Summary
- Graph Definition
- Graph Representation
- Path, Cycle, Tree, Connectivity
- Graph Traversal Algorithms
- Breath first search/traversal
- Application of BFS
40