Sei sulla pagina 1di 38

CS260 Intro to Graphs

DREXEL UNIVERSITY
DEPT. OF COMPUTER SCIENCE

SLIDES BY PROFESSOR ALI SHOKOUFANDEH


ADAPTED FOR CS260 BY MARK BOADY
SUMMER 2016
Graphs
2

A graph G =(V,E) is a Amsterdam 690


Berlin
pair of sets: 205
V: vertex set. 380
Brussels
V={Amsterdam, Berlin, Brussels, Florence,
Frankfurt 530
Frankfurt, Geneva, Marseille, Milan, 405
Munich, Paris, Prague} Paris 290
Prague
E: edge set. 360
585
550
E={(Amsterdam,Berlin), (Amsterdam, Brussels),
(Brussels, Frankfurt),, (Munich, Florence)} Munich
550
Geneva 410
Milan 650
Graph may be 310

weighted and its Marseille 390 250


Florence
edges might be
directed.

Drexel CS Fall, 2013


Preliminaries
3

Things to know: Amsterdam Berlin


690
Path
Cycle 205
380
Sub-graph Brussels
Degree of a Node Frankfurt 530
Maximum and Minimum 405
Degree Paris 290
Prague
Maximum Number of 585 360
Edges in an Undirected 550
Graph Munich
550
Connected Components of Geneva 410
a Graph Milan 650
310
Shortest Path in a
390
Weighted Graph Marseille 250

Tree (rooted tree) Florence


Spanning Tree of a Graph deg(Florence)=2
Acyclic Graph
Bipartite Graph
Drexel CS Fall, 2013
Notations
4

Given A graph G=(V,E), where


V is its vertex set, |V|=n,
E is its edge set, with |E|=m=O(n2).

If G is connected then for every pair of vertices u,v in


G, there is path connecting them.
In an undirected graph, an edge (u,v)=(v,u).
In a directed graph, (u,v) is different from (v,u).
In a weighted graph there are weights associated with
edges and/or vertices.
Running time of graph algorithms are usually
expressed in terms of n or m.
Drexel CS Fall, 2013
Graph Representations: Adjacency Matrix
5

The adjacency matrix of a graph G, G 1


denoted by AG is an n x n matrix
defined as follows: 2 4
#1 if (i, j ) E
AG [i, j ] = " 3
!0 if (i, j ) E
If G is undirected then AG
&0 1 1 0#
is symmetric. $0 0 1 0!!
AG = $
$0 0 0 0!
$ !
%0 0 1 0"

Drexel CS Fall, 2013


Graph Representations: Adjacency Matrix
6

If G is directed, then number of 1s in


G 1
AG is m
If G is undirected, then number of 1s 2 4
in AG is 2m
Degree of a vertex is the sum of entries 3
in corresponding row of AG
If G is undirected then sum of all &0 1 1 0#
degree is 2m $0 0 1 0!!
AG = $
In a directed graph, sum of the out $0
$
0 0 0!
!
degrees is equal to m %0 0 1 0"

Drexel CS Fall, 2013


Graph Representations: Adjacency List
7

For each vertex v in V, a list Adj[v] will represent


vertices that are adjacent to v.
Size of this list is the degree of v.

G 1 Adj[1] = {2,3}
2 4
Adj[2] = {3}
Adj[3] = {}
3
Adj[4] = {3}
Drexel CS Fall, 2013
Depth First Search (DFS)
8

Consider the problem of searching a castle for treasure.


To solve it you might use the following strategy.
As you enter a room of the castle, paint some graffiti on the
wall to remind yourself that you were already there.
Successively travel from room to room as long as you
come to a place you haven't already been.
When you return to the same room, try a different
door leaving the room (assuming it goes somewhere you
haven't already been).
When all doors have been tried in a given room, then
backtrack.

Drexel CS Fall, 2013


Depth First Search (DFS)
9

Notice that this algorithm is described recursively.


In particular, when you enter a new room, you are
beginning a new search.
This is the general idea behind DFS.

Drexel CS Fall, 2013


DFS Algorithm
10

We assume we are given a directed graph G=(V,E).


(same algorithm works for undirected graphs too)
We use four auxiliary arrays.
A color for each vertex:
white means undiscovered,
gray means discovered but not finished processing, and
black means finished.
Predecessor pointers: pointing back to the vertex that
discovered a given vertex.

Drexel CS Fall, 2013


DFS Algorithm
11

Associate two numbers with each vertex as time


stamps.
when we first discover a vertex u store a counter in d[u] and
when we are finished processing a vertex we store a counter
in f[u].
(Note: do not confuse the discovery time d[v] with the
distance d[v] from BFS.)

Drexel CS Fall, 2013


DFS Algorithm
12

To wrap up: four auxiliary arrays are


Color

Predecessor pointers

Discovery time

Finishing time

Drexel CS Fall, 2013


DFS (Contd.)
13

DFS(G) DFSVisit(u)
1 for each u in V 1 color[u] = gray;
2 color[u] = white; 2 d[u] = ++time;
3 pred[u] = nil; 3 for each v in Adj(u)
4 time = 0; 4 if (color[v] == white)
5 for each u in V 5 pred[v] = u;
6 if (color[u] == white) 6 DFSVisit(v);
7 DFSVisit(u); 7 color[u] = black;
8 f[u] = ++time;

Total = (V + E )
Drexel CS Fall, 2013
Example
14
a a a
d e
DFS(a) 1/ 1/ 1/
b a f DFS(b) Return(c) b
b DFS(f) b f
DFS(c) Return(b)
2/ 2/5 DFS(g) 2/5 6/

g
c c c g
3/ 3/4 3/4 7/
c

a DFS(d)
Return(g) DFS(e) a d
Return(f) 1/10 11/14
Return(e) 1/10
Return(a) f Return(d)
b f e
2/5 6/9 b
2/5 6/9 12/13

c g
3/4 7/8 c g
3/4 7/8

Drexel CS Fall, 2013


Parenthesis Structure
15

By looking at the time stamps of two nodes we can


find out about their ancestor/descendent relation:
a d

a d
11/14
1/10 b f
f e
b 12/13
2/5 6/9
c g c

c g
3/4 7/8

1 2 3 4 5 6 7 8 9 10 11 12 13 14
(a (b (c c) b) (f (g g) f) a) (d (c c) d)

Drexel CS Fall, 2013


Shortest Paths
16

Finding the Shortest Paths in a graph arises in many


different application:
Transportation Problems:
Finding the cheapest way to travel between two locations.
Motion Planning:
What is the most natural way to travel a robot in an
environment.
Communication Problems:
The shortest set of hubs to get a message between two nodes in a
network.
Which two locations are farthest apart, i.e. , what is the diameter
of a network.

Drexel CS Fall, 2013


The Single Source Shortest Path
17

We are given a graph G=(V,E) and a real weight


function w from E to R, define the weight of a path
p=<v0, v1,, vk > as the sum of weight of its edges:
k
w( p) = w(v i1,v i ).
i=1

We define the shortest-path weight between u and v by:

w( p) : u p
min{ v} if there is a path between u and v
( u, v ) =
otherwise

Drexel CS Fall, 2013


The Single Source Shortest Path
18

Given a graph G=(V,E), we want to find a shortest


path from a source s to every vertex v in V.
Variants:
Single pair shortest path.
All-pairs shortest path.

Drexel CS Fall, 2013


Non-uniqueness of Shortest Paths
19

u 6 v
3 4
s 1
2 2 7
3
5
6 y
x

u 6 v u v
6
3 9 3 9
3 4 3
s 1 s 4
2 2 7 2 1
0 3 0 2 7
3
5 5 11 5
6 5 11
x y 6 y
x

Drexel CS Fall, 2013


Sub-optimality
20

Given a weighted graph G=(V,E) and weight function w on


edges.
Let P=<v1,v2,,vk> be a shortest path from v1 to vk and for
any i and j such that 1 i j k
Let Pij=<vi,vi+1,,vj> be the sub path of P from vi to vj.
Then, Pij is the shortest path from vi to vj .
Pij

v1 vi vj vk
Pij
Drexel CS Fall, 2013
Sub-optimality
21

Let G=(V,E) be a weighted, directed graph with weight


function w.
Suppose that a shortest path P from source s to a vertex v
can be decomposed into a path P and an edge (u,v) as
follows:
s u v
P
Then the weight of shortest path from s to v is

(s,v) = (s,u) + w(u,v)

Drexel CS Fall, 2013


Triangle Inequality
22

Let G=(V,E) be a weighted, directed graph with weight


function w.
Then for all edges (u,v), we have

(s,v) (s,u) + w(u,v)


(s,u)
s u
(s,v) w(u , v)

v
Drexel CS Fall, 2013
Relaxation
23

For a vertex v in V, we maintain an attribute d[v]:


This is an upper bound on the weight of shortest path from
s to v.
We call d[v] a shortest path estimate.

Initially, all the shortest path estimates are infinity.


As algorithms proceeds this values gets closer and
closer to actual value (s,v) of shortest path
between s and v.


Drexel CS Fall, 2013
Relaxation
24

The process of relaxing an edge (u,v) consists of testing


whether we can improve the shortest known path to v, by
adding (u,v) to the shortest known path to u.
A relaxation step may decrease the shortest path estimate
d[v] using the triangle inequality:

Relax(u,v,w)
if d[v]> d[u]+w(u,v) then
d[v]= d[u]+ w(u,v)
End

Drexel CS Fall, 2013


The Effect of relaxation
25

2 v u 2 v
u 5 6
5 9

Relax(u,v) Relax(u,v)

2 v u 2 v
u 5 6
5 7

Drexel CS Fall, 2013


Algorithm
26

SSSP(G)
for each v V do Initialize of d[ ] to inf
d [v ] =
Relaxation: each vertex can
d [s] = 0
S =
be subject to relaxation as
Q =V many times as its in-degree.
while Q do The changes due to
u = Extract - Min(Q ) relaxation will be handled
S = S {u} by Decrease-Key
for each v Adj[u ] do function.
if d [v] > d [u ] + w(u , v) then
d [v] = d [u ] + w(u , v)

Drexel CS Fall, 2013


Example
27

10 B Q: ( A,0)
2
0
A
4 ( B, ) (C , )
D
4
5 ( D, )
C 1

10 Extract-Min: A
10 B Decrease-Key: B,C
2
0
A
4
D Q:
4 (C ,5)
5
C 1 ( D, ) (B,10)

5
Drexel CS Fall, 2013
Example
28

9
10 B Extract-Min: C
2
0 6 Decrease-Key: B,D
A
4
D
4 Q: (D,6)
5
C 1 (B,9)
5
8
10 B Extract-Min: D
2
0 6 Decrease-Key: B
A
4
D
4 Q: (B,9)
5
C 1
5
Drexel CS Fall, 2013
Example
29

8
10 B Extract-Min: B
2
0 6
A
4
D
3 Q: EMPTY
5
C 1
5

Drexel CS Fall, 2013


Running Time
30

SSSP(G)
for each v V do
Extract-Min:
d [v ] =
|V| times.
d [s] = 0
S = Decrease-Key:
Q =V |E| times.
while Q do
u = Extract - Min(Q )
S = S {u} O(V logV + E logV )
for each v Adj[u ] do
if d [v] > d [u ] + w(u , v) then
d [v] = d [u ] + w(u , v)

Drexel CS Fall, 2013


All-Pairs Shortest Paths
31

Single-Source Shortest Paths:


Compute shortest paths from a given source to all vertices in
the graph.
Dijkstras : O(VlgV + ElgV) with binary heap

All-Pairs Shortest Paths:


Given a graph G = (V,E), |V|=n, and a weight function w on
the edges, compute the shortest paths between all pairs of
vertices.
The problem is not well defined in the presence of a
negative weight cycle in the graph.

Drexel CS Winter, 2014


All-Pairs Shortest Path Algorithms
32

We can solve an all-pairs shortest-paths problem by


running a single source shortest-paths algorithm n
times, once for each vertex as a source.
Then the running time is:
O(V2 log V + VE) if we use the Dijkstras algorithm
We cannot use Dijkstras alg. if there are negative weight edges

Drexel CS Winter, 2014


Floyd-Warshall Algorithm
33

In each iteration, the previous algorithm extends the


number of edges that can be used by the paths.
Floyd-Warshall algorithm extends the set of
vertices that can be used as intermediate vertices
on the paths.
For a path p= <v1 ,v2,, vk-1,vk> the edges v2,,vk-1 are
intermediate vertices.
Let V={1,,n}. In iteration k, the algorithm
computes all pairs shortest paths with intermediate
vertices being in {1,,k}.
Drexel CS Winter, 2014
Floyd-Warshall Algorithm
34

Let d(k)i,j =the distance of a shortest path from i to


j using only vertices of {1,,k}.

all intermediate vertices all intermediate vertices


in {1,2,,k-1} in {1,2,,k-1}

k
p1 p2
i j

all intermediate vertices in {1,2,,k}

Drexel CS Winter, 2014


Floyd-Warshall Algorithm
35

wij , if k = 0
Lemma: d (k )
=
ij
min{dij( k 1) , dikk 1 + d kjk 1} , if k 1

if P does not use vertex k, then dij( k ) = dij( k 1)

Otherwise, P consists of a path P1 from i to k,


followed by a path P2 from k to j.

P1 is a shortest path from i to k in {1,,k-1} and


P2 is a shortest path from k to j in {1,,k-1}
Drexel CS Winter, 2014
Floyd-Warshall Algorithm
36

Floyd-Warshall(W)
1) n = W.rows d is a matrix
with indexes I
2) d ij(0) = copy(W )
and j
3) for k = 0 to n 1 do
Running time:
4) let dij(k ) = copy(dijk1 ) Q(n3)
5) for i = 0 to n 1 do
6) for j = 0 to n 1 do
7) dij(k ) = min(dij(k1), dik(k1) + dkj(k1) )
8) return d (n1)

Drexel CS Winter, 2014


Constructing a Shortest Path
37

Calculate a predecessor matrix

NIL if i = j or w ij =
( 0)
=
ij
i if i j and w ij <

( k 1)
ij if dij( k 1)
dik( k 1)
+ d kj( k 1)
(k )
ij = ( k 1)
kj if dij( k 1)
> dik( k 1)
+ d kj( k 1)

Drexel CS Winter, 2014


Example
38

.
wij , if k = 0 .
d (k )
=
ij
min{dij( k 1) , dikk 1 + d kjk 1} , if k 1 .
Drexel CS Winter, 2014

Potrebbero piacerti anche