Transcript Slide 1

Graphs
• example: model network of computers, cities
connected by (cables, roads)
• G = (V, E)
– V = nodes, vertexes, E = edges between pairs of
nodes
– Captures pairwise relationship between objects
– Graph size parameters: n = |V|, m = |E|
– Undirected graphs: edges are sets of two nodes {u, v}
– Directed graphs: edges are ordered pairs (u, v) DEG
(directed edge graph)
Paths and Connectivity
• 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
• An undirected graph is connected if for every pair of
nodes u and v, there is a path between u and v
• 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
Definitions
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
Connented
However, not connected
When also consider {9, 10, 11, 12, 13}
cycle C = 1, 2, 4, 5, 3, 1
Graph Representation: Adjacency
Matrix
• Adjacency matrix: n-by-n matrix with Auv = 1 if (u,
v) is an edge
– Checking if (u, v) is an edge takes (1) time
– Space proportional to n2
– Identifying all edges incident to a given node takes
(n) time
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
Graph Representation: Adjacency List
• Adjacency list: Node-indexed array of lists
– Checking if (u, v) is an edge takes O(deg(u)) time
– Space proportional to m + n
# incident edges
1
2
3
2
1
3
4
5
3
1
2
5
7
4
2
5
5
2
3
4
6
6
5
7
3
8
8
3
7
8
Graph Traversal
• Assume each edge’s length is 1
• s-t connectivity problem: Given two nodes s and
t, is there a path between s and t?
• s-t shortest path problem: Given two nodes s
and t, what is the length of the shortest path
between s and t?
Breadth First Search
• BFS intuition: Explore outward from s in all possible
directions, adding nodes one "layer" at a time
s
L1
L2
L n-1
• BFS algorithm
– L0 = { s }
– L1 = all neighbors of L0
– L2 = all nodes that do not belong to L0 or L1, and that
have an edge to a node in L1
– Li+1 = all nodes that do not belong to an earlier layer,
and that have an edge to a node in Li
• Theorem: For each i, Li consists of all nodes at distance
exactly i from s. There is a path from s to t iff t appears in
some layer
Breadth First Search
BFS(G, s)
Initialize the attributes
Enqueue(s)
As long as the queue is not empty
Let v be the head of the queue
For each unmarked neighbor u of v
mark u
Distance(u)=Distance(v)+1
Parent(u)=v
Enqueue(u)
Dequeue
Example
Depth First Search
DFS(G, s)
Initialize the attributes
push(s)
As long as the stack is not empty
Let v be the top of the stack,
Pop v
Find the first unmarked neighbor u of v
mark u
Push u
Example
• Breath_First_Travesal
• Depth_First_Travesal
Shortest Path Problem
• Directed graph G = (V, E)
– Source s, destination t
– Length e = length of edge e
• Shortest path problem: find shortest directed
path from s to t
Dijkstra's algorithm
• is a solution to the single-source shortest path
problem in graph theory.
Approach: Greedy
Input: Weighted graph G={E,V}, source vertex s∈V,
all edge weights are nonnegative
Output: Lengths of shortest paths (or the shortest
paths themselves) from a given source vertex
v∈V to all other vertices
Dijkstra's Algorithm
• The idea
– Maintain a set of explored nodes S for which we have
determined the shortest path distance d(u) from s to u
– Initialize S = { s }, d(s) = 0
– Repeatedly choose unexplored node v which
d (u ) + e ,
minimizes p (v) = e = (umin
, v ) : u ÎS
shortest path to some u in explored
part, followed by a single edge (u, v)
add v to S, and set d(v) = (v)
d(u)
u
S
s
e
v
Dijkstra’s Algorithm: Implementation
Dijkstra(G, s)
Initialize the attributes (Distance(s)
= 0,  for all others)
Enqueue all vertices into min-distance priority queue
While the queue is not empty
Dequeue, let v be the element dequeued
For each neighbor u of v that is still in the queue
If Distance(u) > Distance(v) + weight((v,u))
Distance(u) = Distance(v) + weight((v,u))
Parent(u)=v
Demo
• Label each node with the current distance found
so far
• Each step, pick the node with the shortest
distance which is not in the solved set S yet ,
add it to the solved set
• Once u is added to S we update the labels of all
the vertices that are not in S
• To update labels:
D(s, v) = min{D(s, v), D(s, u) + w(u, v)}
Mark v’s parent as u
Demo
Remarks
• Dijkstra’s algorithm is a solution to the singlesource shortest path problem in graph theory
• Bellman-Ford algorithm finds shortest paths in a
graph with negative cost edges (or reports the
existence of a negative cost cycle).