



























































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
[Week 4] Graphs -- Definitions, Representations, BFS, DFS, Directed Graphs
Typology: Lecture notes
1 / 67
This page cannot be seen from the preview
Don't miss anything!




























































Assignment 1: due by 11:59 pm tonight
Quiz 1: tutorial time tonight
After Quiz, Tutorial and Help Desk at PNR Lecture Theatre 310 & 311
Quiz 2 7:00PM Fri 12 April 2019 (Week 7) Assignment 2 11:00PM Fri 12 April 2019
Quiz 3 7:00PM Fri 24 May 2019 (Week 12) Assignment 3 11:00PM Fri 24 May 2019
Teaching Assistant Darcy Townsend [email protected]
Last Week
Slides by Kevin Wayne.Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
transportation
Graph street intersections
Nodes Edges highways communication computers fiber optic cables World Wide Web web pages hyperlinks social people relationships food web species predator-prey
software systems functions function calls
scheduling tasks precedence constraints circuits gates wires
Web graph. Node: web page. Edge: hyperlink from one page to another.
cnn.com
netscape.com novell.com cnnsi.com timewarner.com
hbo.com
sorpranos.com
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 Θ(m + n) time.
1 2 3 2 3 4 2 5 5 6 7 3 8 8
1 3 4 5 1 2 5 7 8
2 3 4 6 5
degree = number of neighbors of u
3 7
Def. A path in an undirected graph G = (V, E) is a sequence P of nodes v 1 , v 2 , …, vk-1 , v (^) k with the property that each consecutive pair v (^) i, v (^) i+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.
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.
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
s-t connectivity problem. Given two node s and t, is there a path between s and t?
s-t shortest path problem. Given two node s and t, what is the length of the shortest path between s and t?
Applications. Many. For example:
BFS intuition. Explore outward from s in all possible directions, adding nodes one "layer" at a time.
BFS algorithm. L 0 = { s }. L 1 = all neighbors of L 0. L 2 = all nodes that do not belong to L 0 or L 1 , and that have an edge to a node in L 1. L (^) i+1 = all nodes that do not belong to an earlier layer, and that have an edge to a node in L (^) i.
Theorem. For each i, L (^) i consists of all nodes at distance exactly i from s. There is a path from s to t iff t appears in some layer.
s (^) L 1 L 2 L (^) n-
Property. Let T be a BFS tree of G = (V, E), and let (x, y) be an edge of G. Then the level of x and y differ by at most 1.
L 0
L 1
L 2
L 3
Theorem. The above implementation of BFS runs in O(m + n) time if the graph is given by its adjacency representation.
Pf. Easy to prove O(n 2 ) running time:
Actually runs in O(m + n) time:
each edge (u, v) is counted exactly twice in sum: once in deg(u) and once in deg(v)