graphs: representation and elementary algorithms · inkscape xfig audit-libs-python policycoreutils...
TRANSCRIPT
Graphs: Representation and
Elementary Algorithms
Antonio Carzaniga
Faculty of InformaticsUniversità della Svizzera italiana
April 23, 2015
© 2007 Antonio Carzaniga
Outline
Graphs: definitions
Representations
Breadth-first search
Depth-first search
© 2007 Antonio Carzaniga
Example
© 2007 Antonio Carzaniga
Example
GE
VS
TI
VD
FR
BE
OW
NW
UR
GR
NE
LU
ZG
SZ
GL
SG
JU
SO
BS
BL
AG
SH
ZH
TG AR
AL
© 2007 Antonio Carzaniga
Same Example (Better Layout)
GE VS TI
VD FR BE
OW
NW
UR GR
NE LU
ZG
SZ
GL
SG
JU
SO
BS BL AG SH
ZH
TG
AR
AL
© 2007 Antonio Carzaniga
Many Examples
Social networks: who knows who
The Web graph: which page links to which
The Internet graph: which router links to which
Citation graphs: who references whose papers
Planar graphs: which country is next to which
Well-shaped meshes: pretty pictures with triangles
Geometric graphs: who is near who
Random graphs: whichever. . .
Examples and descriptions taken from Daniel A. Spielman’s course “Graphs and Networks.”
© 2007 Antonio Carzaniga
Definitions
A graph
G = (V , E)
V is the set of vertices (also called nodes)
E is the set of edges
© 2007 Antonio Carzaniga
Definitions
A graph
G = (V , E)
V is the set of vertices (also called nodes)
E is the set of edges
◮ E ⊆ V × V , i.e., E is a relation between vertices
◮ an edge e = (u, v) ∈ V is a pair of vertices u ∈ V and v ∈ V
© 2007 Antonio Carzaniga
Definitions
A graph
G = (V , E)
V is the set of vertices (also called nodes)
E is the set of edges
◮ E ⊆ V × V , i.e., E is a relation between vertices
◮ an edge e = (u, v) ∈ V is a pair of vertices u ∈ V and v ∈ V
An undirected graph is characterized by a symmetric relation
between vertices
◮ an edge is a set e = {u, v} of two vertices
© 2007 Antonio Carzaniga
Example (2)
0/0
15/0
0/15
30/0
0/30
40/0
0/40
15/15
30/15
15/30
30/30
40/15
15/40
40/30
30/40
game A
game B
deuce
adv. A
adv. B
© 2007 Antonio Carzaniga
Example (3)
perl-HTML-Tagset
perl-HTML-Parser
perl-libwww-perl
gd
libwmf
gcgc-devel
automake
libtool
cyrus-sasl-md5
gaim
xorg-x11-drv-magictouch
xorg-x11-drivers
giflibgiflib-devel
tk tix
ruby-libsruby
bluez-gnome
bluez-utils
xorg-x11-fonts-base
xorg-x11-server-Xorg
xosd
xorg-x11-drv-i810
xorg-x11-drv-i128
xorg-x11-drv-digitaledge
xorg-x11-drv-dummy
xorg-x11-drv-glint
xorg-x11-drv-cirrus xorg-x11-drv-summa
xorg-x11-drv-s3virge
xorg-x11-drv-mutouch
xorg-x11-drv-ast
xorg-x11-drv-calcomp
xorg-x11-drv-chips
xorg-x11-drv-void
xorg-x11-drv-vga
xorg-x11-drv-hyperpen
xorg-x11-drv-s3
xorg-x11-drv-jamstudio
xorg-x11-drv-voodoo
xorg-x11-drv-ati
xorg-x11-drv-vesa
xorg-x11-drv-i740
xorg-x11-drv-spaceorb
xorg-x11-drv-nsc
synaptics
xorg-x11-drv-palmax
xorg-x11-drv-savage
xorg-x11-drv-aiptek
xorg-x11-drv-apm
xorg-x11-drv-tdfx
xorg-x11-drv-keyboard
xorg-x11-drv-neomagic
xorg-x11-drv-cyrix
xorg-x11-drv-v4l
xorg-x11-drv-citron
xorg-x11-drv-vmware
xorg-x11-drv-mouse
xorg-x11-drv-trident
xorg-x11-drv-magellan
xorg-x11-drv-tseng
xorg-x11-drv-dynapro
xorg-x11-drv-elo2300
xorg-x11-drv-sis
xorg-x11-drv-ur98
xorg-x11-drv-joystick
xorg-x11-drv-fpit
xorg-x11-drv-acecad
xorg-x11-drv-sisusb
xorg-x11-drv-microtouch
xorg-x11-drv-ark
xorg-x11-drv-dmc
xorg-x11-drv-elographics
xorg-x11-drv-evdev
xorg-x11-drv-fbdev
xorg-x11-drv-mga
xorg-x11-drv-nv
xorg-x11-drv-penmount
xorg-x11-drv-rendition
xorg-x11-drv-siliconmotion
xorg-x11-drv-tek4957
xorg-x11-drv-via
xorg-x11-drv-vmmouse
xmms-xosd
mkisofsdvd+rw-tools
gtk2
AdobeReader_enu
gail
gconf-editor
gimp
gnome-icon-theme
gnome-keyring
gtk2-devel
gtk2-engines
gtkhtml2
libbonoboui
libgcj
libglade2
libgnomecanvas
libgnomeprintui22
libgnomeui
librsvg2
libwnck
openoffice.org-core
RealPlayer
wireshark-gnome
gimp-print-plugin gimp-data-extras
redhat-artwork
java-1.4.2-gcj-compat
gjdoc
evolution-data-server
openoffice.org-writer
openoffice.org-base openoffice.org-calc
openoffice.org-draw
openoffice.org-graphicfilter
openoffice.org-impress
openoffice.org-langpack-it
lame
libgnomecups
libgnomeprint22
gtk+
gtk+-devel
gtk-engines
imlib
usbview
gnutls
libsoup
basesystem
glibc
j2sdk
jdk
ppp
glibc-devel
bind
pam
openldap
sed
lockdev
glibc-headers
xorg-x11-drv-i810-devel
ghostscript
ghostscript-fonts
ghostscript-gtk
pstoedit
tetex-fontools
transfig
inkscape
xfig
audit-libs-python
policycoreutils
libXcursor-devel
ttmkfdir
xorg-x11-fonts-truetype
xorg-x11-xfs
initscripts
dhclient
dhcpv6_client
hal
ipsec-tools
kbd
mkinitrd
openssh
portmap
privoxy
vte
xinetd
gnome-mount
pm-utils
openssh-clients
openssh-server
libdvdreadlibdvdnav
bluez-libs
xalan-j2
bsf
bsh
libxslt
libgnome
scrollkeeper
xmlsec1
xmlsec1-nss
evince
galeon
gnumeric
libgnomedb
elfutils
rpm-build
libgpg-errorlibgpg-error-devel
kakasi
namazuperl-Text-Kakasi
boostboost-devel
wireshark
gawk
autoconf
ethtool
R
e2fsprogs-libs
e2fsprogs
firefox
zip
libstdc++-devel
gcc-c++
startup-notification
gnome-desktop
metacity
perl
docbook-dtds
git-core
latex2html
lm_sensors
netpbm-progs
perl-Compress-Zlib
perl-DateManip
perl-DBI
perl-Error
perl-Parse-Yappperl-XML-Parser
subversion
swish++
w3m
xscreensaver-base
xscreensaver-extras
git-cvs
git-arch
git-svn
git-email
perl-Git
tetex-latex
subversion-perl
w3m-img
tzdata
glibc-common
m4
bison
bash
bash-completion
gpm
info
sysklogd
tar
gdb
tetex-dvips
tetex-fonts
xboard
libICE
libICE-devel
libSM-devel
dbus-python
avahi
avahi-glib
cairo
cairo-devel
pango
pango-devel
gnome-vfs2
gnome-vfs2-smb
bzip2-libs
bzip2
man
tla
tetex-eurofont
tetex-font-cm-lgc
tetex-font-kerkis
tetex-unicode
atk
atk-devel
logrotate
unzip
xmms
gkrellmms
libgomp
gcc
xerces-j2
gcc32
glib-devel
alsa-lib
alsa-lib-devel
SDL-devel
cryptsetup-luks
aspell-en
aspell
aspell-it
cvsps
zlib-devel
freetype-devel
libmng-devel
libpng-devel
libxml2-devel
libXft-devel
fontconfig-devel
glib2
GConf2
glib2-devel
glib-java
libcroco
libgsf
libnotify
libwpd
nc
shared-mime-info
notification-daemon
hsqldb
libXfixes-devel
pciutils
binutils
tcl
bind-libs
bind-utils
torcs-data-tracks-road
torcs
torcs-data
torcs-data-cars-extra
torcs-data-cars-nascar
torcs-data-tracks-dirt
torcs-data-tracks-oval
torcs-robots
tmpwatch
tetex
lcdf-typetools
coreutils
darcs
dhcp
fontconfig
guile
hicolor-icon-theme
minicom
xorg-x11-xinit
qt
urw-fonts
dejavu-lgc-fonts
bitmap-fonts
fonts-chinese
fonts-hebrew
fonts-hindi
fonts-japanese
fonts-korean
libXft
xorg-x11-fonts-miscxorg-x11-fonts-syriac
xorg-x11-fonts-Type1
SysVinit
passwd
util-linux
usermode
udev
libSM
libXt-devel
python
PyXML
rhpl
rpm-python
tkinter
yum
python-imaging
gnome-mime-data
skype
nspr
nss
libjpeg
libjpeg-devel
xorg-x11-util-macros
libXmu-devel
libXaw-devel
ed
audit-libs
shadow-utils
rpm
tcpdump
gnupg2
libart_lgpl
vim-minimal
cvs
sudo
popplerpoppler-utils
python-elementtree
openjade
libofx
gsynaptics
paps
cups
yum-metadata-parser
openssl
curl
pstack
ImageMagick
ImageMagick-c++
lvm2
mesa-libGLUmesa-libGLU-devel
dictd
dict-web1913
dmraid
mesa-libGLwmesa-libGLw-devel
pkgconfig
libX11-devel
mesa-libGL-devel
libXext-devel
libXrender-devel
libXpm-devel
gstreamer-toolsgstreamer
gstreamer-plugins-good
gstreamer-plugins-base
graphviz
gkrellmgkrellm-freq
gkrellm-volume
gkrellm-wifi
timidity++
SDL_mixer
dbus
NetworkManager-glib
psutils
mesa-libGL
xorg-x11-proto-devel
jpackage-utils
antlr
gnu-crypto
beecrypt
rpm-libs
groff
ffmpeglibquicktime
libXdmcp-devel
libXi-devel
libXinerama-devel
libXrandr-devel
libXv-devel
libXxf86dga-devel
libXxf86vm-devel
dbus-glib
crontabsanacron
libXcursor
libmng
which
htmlview
libcap
ntp
gimp-print
mktemp
grub
gzip
MAKEDEV
mgetty
openssl097a
cracklib-dicts
xorg-x11-resutils
SDL
libdvlibdv-devel
mplayermplayer-fonts
tomcat5-jsp-2.0-api
desktop-file-utils
gqview
redhat-menus
gnome-menus
xorg-x11-utils
libbonobo
perl-NKF
libXfixes
libao
vorbis-tools
grip
ORBit2
findutils
popt
imake
libraw1394libiec61883
less
calc
iptablesiptables-ipv6
perl-File-MMagic
freetype
freetype-demos
cpio
libdaemon
db4
libXfont
patch
net-tools
libvorbis
setup
filesystem
libXxf86dga
libX11
libusb
hwdata
usbutils
device-mapper
kpartx
libpng
gnuplot
maxima
libsemanage
libgcrypt
libattrlibattr-devel libacl-devel
libvolume_id
xorg-x11-server-utils
libacl
libXrandr
cracklib
esound
ncurses
ncurses-devel
libXpm
chkfontpath
zlib
netpbm
libselinux
libselinux-python
cdparanoia-libscdparanoia
libxml2
libxml2-python
vcdimager
expat
samba-common
samba-client
chkconfig
at
cyrus-sasl
iputils
xmms-libs
xmms-cdread
xmms-mp3
pinentry
setarch
maxima-runtime-gcl
cups-libs
module-init-tools
libsepol
gslgsl-devel
libXmu
xml-common
fedora-release-notes
fedora-release
livna-release
mingetty
libpcap
libXdmcp
libXau-devel
cyrus-sasl-lib
cyrus-sasl-plain
rsync
xml-commons-resolver
psmisc
libXaw
tomcat5-servlet-2.4-api
libXau
xorg-x11-drv-via-devel
dmidecode
libXinerama
wget dillo
elfutils-libs
dialog
sgml-common
opensp
libtiff
libtiff-devel
libstdc++
libXxf86vm
rcs
glib
libXi
libXt
libXv
grep
bc
irda-utils
krb5-libs
tcsh
libXext
kernel-headers
tetex-afm
libgcc
libXrender
file
python-sqlite
elfutils-libelf
libogg
readline
diffutils
cpp
nash
audiofilearts
© 2007 Antonio Carzaniga
Graph Representation
How do we represent a graph G = (E ,V) in a computer?
© 2007 Antonio Carzaniga
Graph Representation
How do we represent a graph G = (E ,V) in a computer?
Adjacency-list representation
V = {1,2, . . . |V |}
G consists of an array Adj
A vertex u ∈ V is represented by an element in the array Adj
© 2007 Antonio Carzaniga
Graph Representation
How do we represent a graph G = (E ,V) in a computer?
Adjacency-list representation
V = {1,2, . . . |V |}
G consists of an array Adj
A vertex u ∈ V is represented by an element in the array Adj
Adj[u] is the adjacency list of vertex u
◮ the list of the vertices that are adjacent to u
◮ i.e., the list of all v such that (u, v) ∈ E
© 2007 Antonio Carzaniga
Example
1 2 3 4
5 6 7 8
9 10 11 12
© 2007 Antonio Carzaniga
Example
1 2 3 4
5 6 7 8
9 10 11 12Adj
1 5 6
2 3 7
3 4 7
4 7
5 9
6 2 7 9
7 8 10 11
8 11 12
9 10
10
11 12
12
© 2007 Antonio Carzaniga
Using the Adjacency-List
Adj
1 5 6
2 3 7
3 4 7
4 7
5 9
6 2 7 9
7 8 10 11
8 11 12
9 10
10
11 12
12
© 2007 Antonio Carzaniga
Using the Adjacency-List
Accessing a vertex u? Adj
1 5 6
2 3 7
3 4 7
4 7
5 9
6 2 7 9
7 8 10 11
8 11 12
9 10
10
11 12
12
© 2007 Antonio Carzaniga
Using the Adjacency-List
Accessing a vertex u? O(1)
◮ optimal
Adj
1 5 6
2 3 7
3 4 7
4 7
5 9
6 2 7 9
7 8 10 11
8 11 12
9 10
10
11 12
12
© 2007 Antonio Carzaniga
Using the Adjacency-List
Accessing a vertex u? O(1)
◮ optimal
Iteration through V ?
Adj
1 5 6
2 3 7
3 4 7
4 7
5 9
6 2 7 9
7 8 10 11
8 11 12
9 10
10
11 12
12
© 2007 Antonio Carzaniga
Using the Adjacency-List
Accessing a vertex u? O(1)
◮ optimal
Iteration through V ? Θ(|V |)◮ optimal
Adj
1 5 6
2 3 7
3 4 7
4 7
5 9
6 2 7 9
7 8 10 11
8 11 12
9 10
10
11 12
12
© 2007 Antonio Carzaniga
Using the Adjacency-List
Accessing a vertex u? O(1)
◮ optimal
Iteration through V ? Θ(|V |)◮ optimal
Iteration through E?
Adj
1 5 6
2 3 7
3 4 7
4 7
5 9
6 2 7 9
7 8 10 11
8 11 12
9 10
10
11 12
12
© 2007 Antonio Carzaniga
Using the Adjacency-List
Accessing a vertex u? O(1)
◮ optimal
Iteration through V ? Θ(|V |)◮ optimal
Iteration through E? Θ(|V | + |E|)◮ okay (not optimal)
Adj
1 5 6
2 3 7
3 4 7
4 7
5 9
6 2 7 9
7 8 10 11
8 11 12
9 10
10
11 12
12
© 2007 Antonio Carzaniga
Using the Adjacency-List
Accessing a vertex u? O(1)
◮ optimal
Iteration through V ? Θ(|V |)◮ optimal
Iteration through E? Θ(|V | + |E|)◮ okay (not optimal)
Checking (u, v) ∈ E?
Adj
1 5 6
2 3 7
3 4 7
4 7
5 9
6 2 7 9
7 8 10 11
8 11 12
9 10
10
11 12
12
© 2007 Antonio Carzaniga
Using the Adjacency-List
Accessing a vertex u? O(1)
◮ optimal
Iteration through V ? Θ(|V |)◮ optimal
Iteration through E? Θ(|V | + |E|)◮ okay (not optimal)
Checking (u, v) ∈ E? O(|V |)
Adj
1 5 6
2 3 7
3 4 7
4 7
5 9
6 2 7 9
7 8 10 11
8 11 12
9 10
10
11 12
12
© 2007 Antonio Carzaniga
Using the Adjacency-List
Accessing a vertex u? O(1)
◮ optimal
Iteration through V ? Θ(|V |)◮ optimal
Iteration through E? Θ(|V | + |E|)◮ okay (not optimal)
Checking (u, v) ∈ E? O(|V |)
◮ bad
Adj
1 5 6
2 3 7
3 4 7
4 7
5 9
6 2 7 9
7 8 10 11
8 11 12
9 10
10
11 12
12
© 2007 Antonio Carzaniga
Graph Representation (2)
© 2007 Antonio Carzaniga
Graph Representation (2)
Adjacency-matrix representation
V = {1,2, . . . |V |}
G consists of a |V | × |V | matrix A
A = (aij) such that
aij =
1 if (i, j) ∈ E
0 otherwise
© 2007 Antonio Carzaniga
Example
1 2 3 4
5 6 7 8
9 10 11 12
© 2007 Antonio Carzaniga
Example
1 2 3 4
5 6 7 8
9 10 11 12
1
2
3
4
5
6
7
8
9
10
11
12
1 2 3 4 5 6 7 8 9 10 11 12
© 2007 Antonio Carzaniga
Example
1 2 3 4
5 6 7 8
9 10 11 12
1
2
3
4
5
6
7
8
9
10
11
12
1 2 3 4 5 6 7 8 9 10 11 12
1 1
1 1
1 1
1
1
1 1 1
1 11
1 1
1
1
© 2007 Antonio Carzaniga
Using the Adjacency-Matrix
1
2
3
4
5
6
7
8
9
10
11
12
1 2 3 4 5 6 7 8 9 101112
1 1
1 1
1 1
1
1
1 1 1
1 11
1 1
1
1
© 2007 Antonio Carzaniga
Using the Adjacency-Matrix
Accessing a vertex u?
1
2
3
4
5
6
7
8
9
10
11
12
1 2 3 4 5 6 7 8 9 101112
1 1
1 1
1 1
1
1
1 1 1
1 11
1 1
1
1
© 2007 Antonio Carzaniga
Using the Adjacency-Matrix
Accessing a vertex u? O(1)
◮ optimal 1
2
3
4
5
6
7
8
9
10
11
12
1 2 3 4 5 6 7 8 9 101112
1 1
1 1
1 1
1
1
1 1 1
1 11
1 1
1
1
© 2007 Antonio Carzaniga
Using the Adjacency-Matrix
Accessing a vertex u? O(1)
◮ optimal
Iteration through V ?
1
2
3
4
5
6
7
8
9
10
11
12
1 2 3 4 5 6 7 8 9 101112
1 1
1 1
1 1
1
1
1 1 1
1 11
1 1
1
1
© 2007 Antonio Carzaniga
Using the Adjacency-Matrix
Accessing a vertex u? O(1)
◮ optimal
Iteration through V ? Θ(|V |)◮ optimal
1
2
3
4
5
6
7
8
9
10
11
12
1 2 3 4 5 6 7 8 9 101112
1 1
1 1
1 1
1
1
1 1 1
1 11
1 1
1
1
© 2007 Antonio Carzaniga
Using the Adjacency-Matrix
Accessing a vertex u? O(1)
◮ optimal
Iteration through V ? Θ(|V |)◮ optimal
Iteration through E?
1
2
3
4
5
6
7
8
9
10
11
12
1 2 3 4 5 6 7 8 9 101112
1 1
1 1
1 1
1
1
1 1 1
1 11
1 1
1
1
© 2007 Antonio Carzaniga
Using the Adjacency-Matrix
Accessing a vertex u? O(1)
◮ optimal
Iteration through V ? Θ(|V |)◮ optimal
Iteration through E? Θ(|V |2)◮ possibly very bad
1
2
3
4
5
6
7
8
9
10
11
12
1 2 3 4 5 6 7 8 9 101112
1 1
1 1
1 1
1
1
1 1 1
1 11
1 1
1
1
© 2007 Antonio Carzaniga
Using the Adjacency-Matrix
Accessing a vertex u? O(1)
◮ optimal
Iteration through V ? Θ(|V |)◮ optimal
Iteration through E? Θ(|V |2)◮ possibly very bad
Checking (u, v) ∈ E?
1
2
3
4
5
6
7
8
9
10
11
12
1 2 3 4 5 6 7 8 9 101112
1 1
1 1
1 1
1
1
1 1 1
1 11
1 1
1
1
© 2007 Antonio Carzaniga
Using the Adjacency-Matrix
Accessing a vertex u? O(1)
◮ optimal
Iteration through V ? Θ(|V |)◮ optimal
Iteration through E? Θ(|V |2)◮ possibly very bad
Checking (u, v) ∈ E? O(1)
1
2
3
4
5
6
7
8
9
10
11
12
1 2 3 4 5 6 7 8 9 101112
1 1
1 1
1 1
1
1
1 1 1
1 11
1 1
1
1
© 2007 Antonio Carzaniga
Using the Adjacency-Matrix
Accessing a vertex u? O(1)
◮ optimal
Iteration through V ? Θ(|V |)◮ optimal
Iteration through E? Θ(|V |2)◮ possibly very bad
Checking (u, v) ∈ E? O(1)
◮ optimal
1
2
3
4
5
6
7
8
9
10
11
12
1 2 3 4 5 6 7 8 9 101112
1 1
1 1
1 1
1
1
1 1 1
1 11
1 1
1
1
© 2007 Antonio Carzaniga
Space Complexity
© 2007 Antonio Carzaniga
Space Complexity
Adjacency-list representation
© 2007 Antonio Carzaniga
Space Complexity
Adjacency-list representation
Θ(|V | + |E|)
© 2007 Antonio Carzaniga
Space Complexity
Adjacency-list representation
Θ(|V | + |E|)
optimal
© 2007 Antonio Carzaniga
Space Complexity
Adjacency-list representation
Θ(|V | + |E|)
optimal
Adjacency-matrix representation
© 2007 Antonio Carzaniga
Space Complexity
Adjacency-list representation
Θ(|V | + |E|)
optimal
Adjacency-matrix representation
Θ(|V |2)
© 2007 Antonio Carzaniga
Space Complexity
Adjacency-list representation
Θ(|V | + |E|)
optimal
Adjacency-matrix representation
Θ(|V |2)
possibly very bad
© 2007 Antonio Carzaniga
Space Complexity
Adjacency-list representation
Θ(|V | + |E|)
optimal
Adjacency-matrix representation
Θ(|V |2)
possibly very bad
When is the adjacency-matrix “very bad”?
© 2007 Antonio Carzaniga
Choosing a Graph Representation
Adjacency-list representation
◮ generally good, especially for its optimal space complexity
◮ bad for dense graphs and algorithms that require random
access to edges
◮ preferable for sparse graphs or graphs with low degree
© 2007 Antonio Carzaniga
Choosing a Graph Representation
Adjacency-list representation
◮ generally good, especially for its optimal space complexity
◮ bad for dense graphs and algorithms that require random
access to edges
◮ preferable for sparse graphs or graphs with low degree
Adjacency-matrix representation
◮ suffers from a bad space complexity
◮ good for algorithms that require random access to edges
◮ preferable for dense graphs
© 2007 Antonio Carzaniga
Breadth-First Search
One of the simplest but also a fundamental algorithm
© 2007 Antonio Carzaniga
Breadth-First Search
One of the simplest but also a fundamental algorithm
Input: G = (V , E) and a vertex s ∈ V
◮ explores the graph, touching all vertices that are reachable
from s
◮ iterates through the vertices at increasing distance (edge
distance)
◮ computes the distance of each vertex from s
◮ produces a breadth-first tree rooted at s
◮ works on both directed and undirected graphs
© 2007 Antonio Carzaniga
Example
1 2 3 4
5 6 7 8
9 10 11 12
© 2007 Antonio Carzaniga
Example
1 2 3 4
5 6 7 8
9 10 11 12
© 2007 Antonio Carzaniga
Example
1 2 3 4
5 6 7 8
9 10 11 12
© 2007 Antonio Carzaniga
Example
1 2 3 4
5 6 7 8
9 10 11 12
© 2007 Antonio Carzaniga
Example
1 2 3 4
5 6 7 8
9 10 11 12
© 2007 Antonio Carzaniga
Example
1 2 3 4
5 6 7 8
9 10 11 12
© 2007 Antonio Carzaniga
Example
1 2 3 4
5 6 7 8
9 10 11 12
© 2007 Antonio Carzaniga
Example
1 2 3 4
5 6 7 8
9 10 11 12
© 2007 Antonio Carzaniga
Example
1 2 3 4
5 6 7 8
9 10 11 12
© 2007 Antonio Carzaniga
Example
1 2 3 4
5 6 7 8
9 10 11 12
© 2007 Antonio Carzaniga
Example
1 2 3 4
5 6 7 8
9 10 11 12
© 2007 Antonio Carzaniga
Example
1 2 3 4
5 6 7 8
9 10 11 12
© 2007 Antonio Carzaniga
Example
1 2 3 4
5 6 7 8
9 10 11 12
© 2007 Antonio Carzaniga
Example
1 2 3 4
5 6 7 8
9 10 11 12
© 2007 Antonio Carzaniga
Example
1 2 3 4
5 6 7 8
9 10 11 12
© 2007 Antonio Carzaniga
Example
1 2 3 4
5 6 7 8
9 10 11 12
© 2007 Antonio Carzaniga
Example
1 2 3 4
5 6 7 8
9 10 11 12
© 2007 Antonio Carzaniga
Example
1 2 3 4
5 6 7 8
9 10 11 12
© 2007 Antonio Carzaniga
Example
1 2 3 4
5 6 7 8
9 10 11 12
© 2007 Antonio Carzaniga
Example
1 2 3 4
5 6 7 8
9 10 11 12
© 2007 Antonio Carzaniga
Example
1 2 3 4
5 6 7 8
9 10 11 12
© 2007 Antonio Carzaniga
Example
1 2 3 4
5 6 7 8
9 10 11 12
© 2007 Antonio Carzaniga
Example
1 2 3 4
5 6 7 8
9 10 11 12
© 2007 Antonio Carzaniga
Example
1 2 3 4
5 6 7 8
9 10 11 12
© 2007 Antonio Carzaniga
Example
1 2 3 4
5 6 7 8
9 10 11 12
© 2007 Antonio Carzaniga
BFS Algorithm
BFS(G, s)
1 for each vertex u ∈ V(G) \ {s}
2 color[u] = white
3 d[u] = ∞
4 π[u] = nil
5 color[s] = gray
6 d[s] = 0
7 π[s] = nil
8 Q = ∅
9 Enqueue(Q, s)
10 while Q 6= ∅
11 u = Dequeue(Q)
12 for each v ∈ Adj[u]
13 if color[v] == white
14 color[v] = gray
15 d[v] = d[u]+ 1
16 π[v] = u
17 Enqueue(Q, v)
18 color[u] = black
1 2 3 4
5 6 7 8
9 10 11 12
© 2007 Antonio Carzaniga
BFS Algorithm
BFS(G, s)
1 for each vertex u ∈ V(G) \ {s}
2 color[u] = white
3 d[u] = ∞
4 π[u] = nil
5 color[s] = gray
6 d[s] = 0
7 π[s] = nil
8 Q = ∅
9 Enqueue(Q, s)
10 while Q 6= ∅
11 u = Dequeue(Q)
12 for each v ∈ Adj[u]
13 if color[v] == white
14 color[v] = gray
15 d[v] = d[u]+ 1
16 π[v] = u
17 Enqueue(Q, v)
18 color[u] = black
1 2 3 4
5 6 7 8
9 10 11 12
u = 1
Q = ∅
© 2007 Antonio Carzaniga
BFS Algorithm
BFS(G, s)
1 for each vertex u ∈ V(G) \ {s}
2 color[u] = white
3 d[u] = ∞
4 π[u] = nil
5 color[s] = gray
6 d[s] = 0
7 π[s] = nil
8 Q = ∅
9 Enqueue(Q, s)
10 while Q 6= ∅
11 u = Dequeue(Q)
12 for each v ∈ Adj[u]
13 if color[v] == white
14 color[v] = gray
15 d[v] = d[u]+ 1
16 π[v] = u
17 Enqueue(Q, v)
18 color[u] = black
1 2 3 4
5 6 7 8
9 10 11 12
u = 1
Q = {5}
© 2007 Antonio Carzaniga
BFS Algorithm
BFS(G, s)
1 for each vertex u ∈ V(G) \ {s}
2 color[u] = white
3 d[u] = ∞
4 π[u] = nil
5 color[s] = gray
6 d[s] = 0
7 π[s] = nil
8 Q = ∅
9 Enqueue(Q, s)
10 while Q 6= ∅
11 u = Dequeue(Q)
12 for each v ∈ Adj[u]
13 if color[v] == white
14 color[v] = gray
15 d[v] = d[u]+ 1
16 π[v] = u
17 Enqueue(Q, v)
18 color[u] = black
1 2 3 4
5 6 7 8
9 10 11 12
u = 1
Q = {5,6}
© 2007 Antonio Carzaniga
BFS Algorithm
BFS(G, s)
1 for each vertex u ∈ V(G) \ {s}
2 color[u] = white
3 d[u] = ∞
4 π[u] = nil
5 color[s] = gray
6 d[s] = 0
7 π[s] = nil
8 Q = ∅
9 Enqueue(Q, s)
10 while Q 6= ∅
11 u = Dequeue(Q)
12 for each v ∈ Adj[u]
13 if color[v] == white
14 color[v] = gray
15 d[v] = d[u]+ 1
16 π[v] = u
17 Enqueue(Q, v)
18 color[u] = black
1 2 3 4
5 6 7 8
9 10 11 12
u = 5
Q = {6}
© 2007 Antonio Carzaniga
BFS Algorithm
BFS(G, s)
1 for each vertex u ∈ V(G) \ {s}
2 color[u] = white
3 d[u] = ∞
4 π[u] = nil
5 color[s] = gray
6 d[s] = 0
7 π[s] = nil
8 Q = ∅
9 Enqueue(Q, s)
10 while Q 6= ∅
11 u = Dequeue(Q)
12 for each v ∈ Adj[u]
13 if color[v] == white
14 color[v] = gray
15 d[v] = d[u]+ 1
16 π[v] = u
17 Enqueue(Q, v)
18 color[u] = black
1 2 3 4
5 6 7 8
9 10 11 12
u = 5
Q = {6,9}
© 2007 Antonio Carzaniga
BFS Algorithm
BFS(G, s)
1 for each vertex u ∈ V(G) \ {s}
2 color[u] = white
3 d[u] = ∞
4 π[u] = nil
5 color[s] = gray
6 d[s] = 0
7 π[s] = nil
8 Q = ∅
9 Enqueue(Q, s)
10 while Q 6= ∅
11 u = Dequeue(Q)
12 for each v ∈ Adj[u]
13 if color[v] == white
14 color[v] = gray
15 d[v] = d[u]+ 1
16 π[v] = u
17 Enqueue(Q, v)
18 color[u] = black
1 2 3 4
5 6 7 8
9 10 11 12
u = 6
Q = {9}
© 2007 Antonio Carzaniga
BFS Algorithm
BFS(G, s)
1 for each vertex u ∈ V(G) \ {s}
2 color[u] = white
3 d[u] = ∞
4 π[u] = nil
5 color[s] = gray
6 d[s] = 0
7 π[s] = nil
8 Q = ∅
9 Enqueue(Q, s)
10 while Q 6= ∅
11 u = Dequeue(Q)
12 for each v ∈ Adj[u]
13 if color[v] == white
14 color[v] = gray
15 d[v] = d[u]+ 1
16 π[v] = u
17 Enqueue(Q, v)
18 color[u] = black
1 2 3 4
5 6 7 8
9 10 11 12
u = 6
Q = {9,2,7}
© 2007 Antonio Carzaniga
BFS Algorithm
BFS(G, s)
1 for each vertex u ∈ V(G) \ {s}
2 color[u] = white
3 d[u] = ∞
4 π[u] = nil
5 color[s] = gray
6 d[s] = 0
7 π[s] = nil
8 Q = ∅
9 Enqueue(Q, s)
10 while Q 6= ∅
11 u = Dequeue(Q)
12 for each v ∈ Adj[u]
13 if color[v] == white
14 color[v] = gray
15 d[v] = d[u]+ 1
16 π[v] = u
17 Enqueue(Q, v)
18 color[u] = black
1 2 3 4
5 6 7 8
9 10 11 12
u = 6
Q = {9,2,7}
© 2007 Antonio Carzaniga
BFS Algorithm
BFS(G, s)
1 for each vertex u ∈ V(G) \ {s}
2 color[u] = white
3 d[u] = ∞
4 π[u] = nil
5 color[s] = gray
6 d[s] = 0
7 π[s] = nil
8 Q = ∅
9 Enqueue(Q, s)
10 while Q 6= ∅
11 u = Dequeue(Q)
12 for each v ∈ Adj[u]
13 if color[v] == white
14 color[v] = gray
15 d[v] = d[u]+ 1
16 π[v] = u
17 Enqueue(Q, v)
18 color[u] = black
1 2 3 4
5 6 7 8
9 10 11 12
u = 9
Q = {2,7}
© 2007 Antonio Carzaniga
BFS Algorithm
BFS(G, s)
1 for each vertex u ∈ V(G) \ {s}
2 color[u] = white
3 d[u] = ∞
4 π[u] = nil
5 color[s] = gray
6 d[s] = 0
7 π[s] = nil
8 Q = ∅
9 Enqueue(Q, s)
10 while Q 6= ∅
11 u = Dequeue(Q)
12 for each v ∈ Adj[u]
13 if color[v] == white
14 color[v] = gray
15 d[v] = d[u]+ 1
16 π[v] = u
17 Enqueue(Q, v)
18 color[u] = black
1 2 3 4
5 6 7 8
9 10 11 12
u = 9
Q = {2,7,10}
© 2007 Antonio Carzaniga
BFS Algorithm
BFS(G, s)
1 for each vertex u ∈ V(G) \ {s}
2 color[u] = white
3 d[u] = ∞
4 π[u] = nil
5 color[s] = gray
6 d[s] = 0
7 π[s] = nil
8 Q = ∅
9 Enqueue(Q, s)
10 while Q 6= ∅
11 u = Dequeue(Q)
12 for each v ∈ Adj[u]
13 if color[v] == white
14 color[v] = gray
15 d[v] = d[u]+ 1
16 π[v] = u
17 Enqueue(Q, v)
18 color[u] = black
1 2 3 4
5 6 7 8
9 10 11 12
u = 2
Q = {7,10}
© 2007 Antonio Carzaniga
BFS Algorithm
BFS(G, s)
1 for each vertex u ∈ V(G) \ {s}
2 color[u] = white
3 d[u] = ∞
4 π[u] = nil
5 color[s] = gray
6 d[s] = 0
7 π[s] = nil
8 Q = ∅
9 Enqueue(Q, s)
10 while Q 6= ∅
11 u = Dequeue(Q)
12 for each v ∈ Adj[u]
13 if color[v] == white
14 color[v] = gray
15 d[v] = d[u]+ 1
16 π[v] = u
17 Enqueue(Q, v)
18 color[u] = black
1 2 3 4
5 6 7 8
9 10 11 12
u = 2
Q = {7,10,3}
© 2007 Antonio Carzaniga
BFS Algorithm
BFS(G, s)
1 for each vertex u ∈ V(G) \ {s}
2 color[u] = white
3 d[u] = ∞
4 π[u] = nil
5 color[s] = gray
6 d[s] = 0
7 π[s] = nil
8 Q = ∅
9 Enqueue(Q, s)
10 while Q 6= ∅
11 u = Dequeue(Q)
12 for each v ∈ Adj[u]
13 if color[v] == white
14 color[v] = gray
15 d[v] = d[u]+ 1
16 π[v] = u
17 Enqueue(Q, v)
18 color[u] = black
1 2 3 4
5 6 7 8
9 10 11 12
u = 7
Q = {10,3}
© 2007 Antonio Carzaniga
BFS Algorithm
BFS(G, s)
1 for each vertex u ∈ V(G) \ {s}
2 color[u] = white
3 d[u] = ∞
4 π[u] = nil
5 color[s] = gray
6 d[s] = 0
7 π[s] = nil
8 Q = ∅
9 Enqueue(Q, s)
10 while Q 6= ∅
11 u = Dequeue(Q)
12 for each v ∈ Adj[u]
13 if color[v] == white
14 color[v] = gray
15 d[v] = d[u]+ 1
16 π[v] = u
17 Enqueue(Q, v)
18 color[u] = black
1 2 3 4
5 6 7 8
9 10 11 12
u = 7
Q = {10,3,8}
© 2007 Antonio Carzaniga
BFS Algorithm
BFS(G, s)
1 for each vertex u ∈ V(G) \ {s}
2 color[u] = white
3 d[u] = ∞
4 π[u] = nil
5 color[s] = gray
6 d[s] = 0
7 π[s] = nil
8 Q = ∅
9 Enqueue(Q, s)
10 while Q 6= ∅
11 u = Dequeue(Q)
12 for each v ∈ Adj[u]
13 if color[v] == white
14 color[v] = gray
15 d[v] = d[u]+ 1
16 π[v] = u
17 Enqueue(Q, v)
18 color[u] = black
1 2 3 4
5 6 7 8
9 10 11 12
u = 7
Q = {10,3,8,11}
© 2007 Antonio Carzaniga
BFS Algorithm
BFS(G, s)
1 for each vertex u ∈ V(G) \ {s}
2 color[u] = white
3 d[u] = ∞
4 π[u] = nil
5 color[s] = gray
6 d[s] = 0
7 π[s] = nil
8 Q = ∅
9 Enqueue(Q, s)
10 while Q 6= ∅
11 u = Dequeue(Q)
12 for each v ∈ Adj[u]
13 if color[v] == white
14 color[v] = gray
15 d[v] = d[u]+ 1
16 π[v] = u
17 Enqueue(Q, v)
18 color[u] = black
1 2 3 4
5 6 7 8
9 10 11 12
u = 10
Q = {3,8,11}
© 2007 Antonio Carzaniga
BFS Algorithm
BFS(G, s)
1 for each vertex u ∈ V(G) \ {s}
2 color[u] = white
3 d[u] = ∞
4 π[u] = nil
5 color[s] = gray
6 d[s] = 0
7 π[s] = nil
8 Q = ∅
9 Enqueue(Q, s)
10 while Q 6= ∅
11 u = Dequeue(Q)
12 for each v ∈ Adj[u]
13 if color[v] == white
14 color[v] = gray
15 d[v] = d[u]+ 1
16 π[v] = u
17 Enqueue(Q, v)
18 color[u] = black
1 2 3 4
5 6 7 8
9 10 11 12
u = 3
Q = {8,11}
© 2007 Antonio Carzaniga
BFS Algorithm
BFS(G, s)
1 for each vertex u ∈ V(G) \ {s}
2 color[u] = white
3 d[u] = ∞
4 π[u] = nil
5 color[s] = gray
6 d[s] = 0
7 π[s] = nil
8 Q = ∅
9 Enqueue(Q, s)
10 while Q 6= ∅
11 u = Dequeue(Q)
12 for each v ∈ Adj[u]
13 if color[v] == white
14 color[v] = gray
15 d[v] = d[u]+ 1
16 π[v] = u
17 Enqueue(Q, v)
18 color[u] = black
1 2 3 4
5 6 7 8
9 10 11 12
u = 3
Q = {8,11,4}
© 2007 Antonio Carzaniga
BFS Algorithm
BFS(G, s)
1 for each vertex u ∈ V(G) \ {s}
2 color[u] = white
3 d[u] = ∞
4 π[u] = nil
5 color[s] = gray
6 d[s] = 0
7 π[s] = nil
8 Q = ∅
9 Enqueue(Q, s)
10 while Q 6= ∅
11 u = Dequeue(Q)
12 for each v ∈ Adj[u]
13 if color[v] == white
14 color[v] = gray
15 d[v] = d[u]+ 1
16 π[v] = u
17 Enqueue(Q, v)
18 color[u] = black
1 2 3 4
5 6 7 8
9 10 11 12
u = 8
Q = {11,4}
© 2007 Antonio Carzaniga
BFS Algorithm
BFS(G, s)
1 for each vertex u ∈ V(G) \ {s}
2 color[u] = white
3 d[u] = ∞
4 π[u] = nil
5 color[s] = gray
6 d[s] = 0
7 π[s] = nil
8 Q = ∅
9 Enqueue(Q, s)
10 while Q 6= ∅
11 u = Dequeue(Q)
12 for each v ∈ Adj[u]
13 if color[v] == white
14 color[v] = gray
15 d[v] = d[u]+ 1
16 π[v] = u
17 Enqueue(Q, v)
18 color[u] = black
1 2 3 4
5 6 7 8
9 10 11 12
u = 8
Q = {11,4,12}
© 2007 Antonio Carzaniga
BFS Algorithm
BFS(G, s)
1 for each vertex u ∈ V(G) \ {s}
2 color[u] = white
3 d[u] = ∞
4 π[u] = nil
5 color[s] = gray
6 d[s] = 0
7 π[s] = nil
8 Q = ∅
9 Enqueue(Q, s)
10 while Q 6= ∅
11 u = Dequeue(Q)
12 for each v ∈ Adj[u]
13 if color[v] == white
14 color[v] = gray
15 d[v] = d[u]+ 1
16 π[v] = u
17 Enqueue(Q, v)
18 color[u] = black
1 2 3 4
5 6 7 8
9 10 11 12
u = 11
Q = {4,12}
© 2007 Antonio Carzaniga
BFS Algorithm
BFS(G, s)
1 for each vertex u ∈ V(G) \ {s}
2 color[u] = white
3 d[u] = ∞
4 π[u] = nil
5 color[s] = gray
6 d[s] = 0
7 π[s] = nil
8 Q = ∅
9 Enqueue(Q, s)
10 while Q 6= ∅
11 u = Dequeue(Q)
12 for each v ∈ Adj[u]
13 if color[v] == white
14 color[v] = gray
15 d[v] = d[u]+ 1
16 π[v] = u
17 Enqueue(Q, v)
18 color[u] = black
1 2 3 4
5 6 7 8
9 10 11 12
u = 4
Q = {12}
© 2007 Antonio Carzaniga
BFS Algorithm
BFS(G, s)
1 for each vertex u ∈ V(G) \ {s}
2 color[u] = white
3 d[u] = ∞
4 π[u] = nil
5 color[s] = gray
6 d[s] = 0
7 π[s] = nil
8 Q = ∅
9 Enqueue(Q, s)
10 while Q 6= ∅
11 u = Dequeue(Q)
12 for each v ∈ Adj[u]
13 if color[v] == white
14 color[v] = gray
15 d[v] = d[u]+ 1
16 π[v] = u
17 Enqueue(Q, v)
18 color[u] = black
1 2 3 4
5 6 7 8
9 10 11 12
u = 12
Q = ∅
© 2007 Antonio Carzaniga
BFS Algorithm
BFS(G, s)
1 for each vertex u ∈ V(G) \ {s}
2 color[u] = white
3 d[u] = ∞
4 π[u] = nil
5 color[s] = gray
6 d[s] = 0
7 π[s] = nil
8 Q = ∅
9 Enqueue(Q, s)
10 while Q 6= ∅
11 u = Dequeue(Q)
12 for each v ∈ Adj[u]
13 if color[v] == white
14 color[v] = gray
15 d[v] = d[u]+ 1
16 π[v] = u
17 Enqueue(Q, v)
18 color[u] = black
1 2 3 4
5 6 7 8
9 10 11 12
© 2007 Antonio Carzaniga
Complexity of BFS
BFS(G, s)
1 for each vertex u ∈ V(G) \ {s}
2 color[u] = white
3 d[u] = ∞
4 π[u] = nil
5 color[s] = gray
6 d[s] = 0
7 π[s] = nil
8 Q = ∅
9 Enqueue(Q, s)
10 while Q 6= ∅
11 u = Dequeue(Q)
12 for each v ∈ Adj[u]
13 if color[v] == white
14 color[v] = gray
15 d[v] = d[u]+ 1
16 π[v] = u
17 Enqueue(Q, v)
18 color[u] = black
© 2007 Antonio Carzaniga
Complexity of BFS
BFS(G, s)
1 for each vertex u ∈ V(G) \ {s}
2 color[u] = white
3 d[u] = ∞
4 π[u] = nil
5 color[s] = gray
6 d[s] = 0
7 π[s] = nil
8 Q = ∅
9 Enqueue(Q, s)
10 while Q 6= ∅
11 u = Dequeue(Q)
12 for each v ∈ Adj[u]
13 if color[v] == white
14 color[v] = gray
15 d[v] = d[u]+ 1
16 π[v] = u
17 Enqueue(Q, v)
18 color[u] = black
We enqueue a vertex
only if it is white, and
we immediately color
it gray; thus, we
enqueue every vertex
at most once
© 2007 Antonio Carzaniga
Complexity of BFS
BFS(G, s)
1 for each vertex u ∈ V(G) \ {s}
2 color[u] = white
3 d[u] = ∞
4 π[u] = nil
5 color[s] = gray
6 d[s] = 0
7 π[s] = nil
8 Q = ∅
9 Enqueue(Q, s)
10 while Q 6= ∅
11 u = Dequeue(Q)
12 for each v ∈ Adj[u]
13 if color[v] == white
14 color[v] = gray
15 d[v] = d[u]+ 1
16 π[v] = u
17 Enqueue(Q, v)
18 color[u] = black
We enqueue a vertex
only if it is white, and
we immediately color
it gray; thus, we
enqueue every vertex
at most once
So, the (dequeue)
while loop executes
O(|V |) times
© 2007 Antonio Carzaniga
Complexity of BFS
BFS(G, s)
1 for each vertex u ∈ V(G) \ {s}
2 color[u] = white
3 d[u] = ∞
4 π[u] = nil
5 color[s] = gray
6 d[s] = 0
7 π[s] = nil
8 Q = ∅
9 Enqueue(Q, s)
10 while Q 6= ∅
11 u = Dequeue(Q)
12 for each v ∈ Adj[u]
13 if color[v] == white
14 color[v] = gray
15 d[v] = d[u]+ 1
16 π[v] = u
17 Enqueue(Q, v)
18 color[u] = black
We enqueue a vertex
only if it is white, and
we immediately color
it gray; thus, we
enqueue every vertex
at most once
So, the (dequeue)
while loop executes
O(|V |) times
For each vertex u, the
inner loop executes
Θ(|Eu|), for a total of
O(|E|) steps
© 2007 Antonio Carzaniga
Complexity of BFS
BFS(G, s)
1 for each vertex u ∈ V(G) \ {s}
2 color[u] = white
3 d[u] = ∞
4 π[u] = nil
5 color[s] = gray
6 d[s] = 0
7 π[s] = nil
8 Q = ∅
9 Enqueue(Q, s)
10 while Q 6= ∅
11 u = Dequeue(Q)
12 for each v ∈ Adj[u]
13 if color[v] == white
14 color[v] = gray
15 d[v] = d[u]+ 1
16 π[v] = u
17 Enqueue(Q, v)
18 color[u] = black
We enqueue a vertex
only if it is white, and
we immediately color
it gray; thus, we
enqueue every vertex
at most once
So, the (dequeue)
while loop executes
O(|V |) times
For each vertex u, the
inner loop executes
Θ(|Eu|), for a total of
O(|E|) steps
So, O(|V | + |E|)
© 2007 Antonio Carzaniga
Depth-First Search
© 2007 Antonio Carzaniga
Depth-First Search
Immediately follow the links of the most recently-visited
vertex, then backtrack when you reach a dead-end
◮ i.e., backtrack when the current vertex has no more adjacent
vertices that have not yet been visited
© 2007 Antonio Carzaniga
Depth-First Search
Immediately follow the links of the most recently-visited
vertex, then backtrack when you reach a dead-end
◮ i.e., backtrack when the current vertex has no more adjacent
vertices that have not yet been visited
Input: G = (V , E)
◮ explores the graph, touching all vertices
© 2007 Antonio Carzaniga
Depth-First Search
Immediately follow the links of the most recently-visited
vertex, then backtrack when you reach a dead-end
◮ i.e., backtrack when the current vertex has no more adjacent
vertices that have not yet been visited
Input: G = (V , E)
◮ explores the graph, touching all vertices
◮ produces a depth-first forest, consisting of all the depth-first
trees defined by the DFS exploration
© 2007 Antonio Carzaniga
Depth-First Search
Immediately follow the links of the most recently-visited
vertex, then backtrack when you reach a dead-end
◮ i.e., backtrack when the current vertex has no more adjacent
vertices that have not yet been visited
Input: G = (V , E)
◮ explores the graph, touching all vertices
◮ produces a depth-first forest, consisting of all the depth-first
trees defined by the DFS exploration
◮ associates two time-stamps to each vertex
◮ d[u] records when u is first discovered
◮ f [u] records when DFS finishes examining u’s edges, and
therefore backtracks from u
© 2007 Antonio Carzaniga
DFS Algorithm
DFS(G)
1 for each vertex u ∈ V(G)
2 color[u] = white
3 π[u] = nil
4 time = 0 // “global” variable
5 for each vertex u ∈ V(G)
6 if color[u] == white
7 DFS-Visit(u)
DFS-Visit(u)
1 color[u] = grey
2 time = time+ 1
3 d[u] = time
4 for each v ∈ Adj[u]
5 if color[v] == white
6 π[v] = u
7 DFS-Visit(v)
8 color[u] = black
9 time = time+ 1
10 f [u] = time
© 2007 Antonio Carzaniga
Complexity of DFS
© 2007 Antonio Carzaniga
Complexity of DFS
The loop in DFS-Visit(u) (lines 4–7) accounts for Θ(|Eu|)
© 2007 Antonio Carzaniga
Complexity of DFS
The loop in DFS-Visit(u) (lines 4–7) accounts for Θ(|Eu|)
We call DFS-Visit(u) once for each vertex u
◮ either in DFS, or recursively in DFS-Visit
◮ because we call it only if color[u] = white, but then we
immediately set color[u] = grey
© 2007 Antonio Carzaniga
Complexity of DFS
The loop in DFS-Visit(u) (lines 4–7) accounts for Θ(|Eu|)
We call DFS-Visit(u) once for each vertex u
◮ either in DFS, or recursively in DFS-Visit
◮ because we call it only if color[u] = white, but then we
immediately set color[u] = grey
So, the overall complexity is Θ(|V | + |E|)
© 2007 Antonio Carzaniga
Applications of DFS: Topological Sort
© 2007 Antonio Carzaniga
Applications of DFS: Topological Sort
Given a directed acyclic graph (DAG)
◮ find an ordering of vertices such that you only end up with
forward links
© 2007 Antonio Carzaniga
Applications of DFS: Topological Sort
Given a directed acyclic graph (DAG)
◮ find an ordering of vertices such that you only end up with
forward links
Example: dependencies in software packages
◮ find an installation order for a set of software packages
◮ such that every package is installed only after all the packages
it depends on
© 2007 Antonio Carzaniga
Topological Sort Algorithm
© 2007 Antonio Carzaniga
Topological Sort Algorithm
Topological-Sort(G)
1 DFS(G)
2 output V sorted in reverse order of f [·]
© 2007 Antonio Carzaniga
Topological Sort Algorithm
Topological-Sort(G)
1 DFS(G)
2 output V sorted in reverse order of f [·]
shirt
tie
jacket
briefs/boxers
pants
belt
socks
shoes
watch
© 2007 Antonio Carzaniga