Upload
others
View
1
Download
0
Embed Size (px)
Citation preview
Graphs
Slide credits: ▪ K. Wayne, Princeton U.
1
These are not Graphs
EastWestNorth
...not the kind we mean, anyway
2
These are Graphs
K5 K3,3
=
3
4
Undirected Graphs
Undirected graph. G = (V, E) is an ordered pair consisting of � V = nodes. � E = edges between pairs of nodes. � Captures pairwise relationship between objects. � Graph size parameters: n = |V|, m = |E|. � Two nodes connected by an edge are said to be neighbor.
V = { 1, 2, 3, 4, 5, 6, 7, 8 } E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6 } n = 8 m = 11
5
Some Properties of Undirected Graph
� |E|= O(V2)
� If G is connected |E|≥|V|–1
– An undirected graph is connected if for every pair of nodes u and
v, there is a path between u and v.
� deg(V) is defined as the number of edges incident to V
– Handshaking lemma: Σv∈V deg(v) = 2|E|
6
Directed Graphs
Directed graph. G = (V, E) � Edge (u, v) goes from node u to node v.
Ex. Web graph - hyperlink points from one web page to another. � Directedness of graph is crucial. � Modern web search engines exploit hyperlink structure to rank web
pages by importance.
7
World Wide Web
Web graph. � Node: web page. � Edge: hyperlink from one page to another.
cnn.com
cnnsi.comnovell.comnetscape.com timewarner.com
hbo.com
sorpranos.com
8
Paths and Connectivity
Def. A path in an undirected graph G = (V, E) is a sequence P of nodes v1, v2, …, vk-1, vk with the property that each consecutive pair vi, vi+1 is joined by an edge in E.
Def. A path is simple if all nodes are distinct.
Def. An undirected graph is connected if for every pair of nodes u and v, there is a path between u and v.
9
Cycles
Def. A cycle is a path v1, v2, …, vk-1, vk in which v1 = vk, k > 2, and the first k-1 nodes are all distinct.
cycle C = 1-2-4-5-3-1
10
Trees
Def. An undirected graph is a tree if it is connected and does not contain a cycle.
Theorem. Let G be an undirected graph on n nodes. Any two of the following statements imply the third. � G is connected. � G does not contain a cycle. � G has n-1 edges.
11
Rooted Trees
Rooted tree. Given a tree T, choose a root node r and orient each edge away from r.
Importance. Models hierarchical structure.
a tree the same tree, rooted at 1
v
parent of v
child of v
root r
12
13
14
15
16
17
Graph Representation: Adjacency Matrix
Adjacency matrix. n-by-n matrix with Auv = 1 if (u, v) is an edge. � Two representations of each edge. � Space proportional to n2. � Checking if (u, v) is an edge takes O(1) time. � Identifying all edges takes O(n2) time. � Use for dense graph
1 2 3 4 5 6 7 8 1 0 1 1 0 0 0 0 0 2 1 0 1 1 1 0 0 0 3 1 1 0 0 1 0 1 1 4 0 1 0 1 1 0 0 0 5 0 1 1 1 0 1 0 0 6 0 0 0 0 1 0 0 0 7 0 0 1 0 0 0 0 1 8 0 0 1 0 0 0 1 0
18
Graph Representation: Adjacency List
Adjacency list. Node indexed array of lists. � Two representations of each edge. � Space proportional to m + n. � Checking if (u, v) is an edge takes O(deg(u)) time. � Identifying all edges takes O(m + n) time. � Use for sparse graph
1 2 3
2
3
4 2 5
5
6
7 3 8
8
1 3 4 5
1 2 5 87
2 3 4 6
5
degree = number of neighbors of u
3 7
19
Graph Traversal
� Problem: Search for a certain node or traverse all nodes in the graph
� Depth First Search – Once a possible path is found, continue the search until the
end of the path � Breadth First Search
– Start several paths at a time, and advance in each one step at a time
20
Depth First Traversal
� A natural way to do Depth-first search (BFS) is using recursion.
DFS( Node c ) { Mark c "Visited” For each neighbor n of c If n "Unvisited" DFS( n ) }
Possible visit sequence: 1, 2, 4, 5, 6, 3, 8, 7
21
Breadth First Traversal
� Breadth-first search (BFS) not naturally recursive. � Use a queue so that vertices are visited in order according to
their distance from the starting vertex.
BFS(Node v) { create a queue Q enqueue v onto Q mark v “Visited” while Q is not empty { dequeue t from Q for each neighbor u of t do if u is not marked { mark u “Visited” enqueue u onto Q } }
22
Breadth First Search
L0
L1
L2
L3
Visit sequence: 1, 2, 3, 4, 5, 7, 8, 6
23
Applications: Finding a Path
� Find path from source vertex s to destination vertex d
� Use graph search starting at s and terminating as soon as we reach d
� Need to remember edges traversed
� Use depth – first search
24
DFS
EF
G
B
CD
A start
destination
A DFS on A ADFS on BB
A
DFS on CBC
AB Return to call on BD Call DFS on D
ABD
Call DFS on GG found destination - done! Path is implicitly stored in DFS recursion Path is: A, B, D, G
DFS Process
25
26
27
28
29
30
31
What We Have Learned
� Graph abstraction capturing objects and their relationships
� Graph representation with adjacency matrix and adjacently list
� Operations on graphs:
� BFS
� DFS
� Applications of BFS and DFS
32