Вы находитесь на странице: 1из 54

Graphs

Breadth-first Search (BFS)


Breadth-first search is one of the simplest algorithms
for searching a graph.
Used in many important graph applications
Prims minimum-spanning tree algorithm and

Dijkstras single-source shortest-paths algorithm


BFS : How does it work?
Given a graph = (, ) and a distinguished source
vertex
breadth-first search systematically explores the
edges of G to discover every vertex that is
reachable from s.
It computes the distance (smallest number of edges)
from s to each reachable vertex.
BFS : How does it work?
It also produces a breadth-first tree with root s that
contains all reachable vertices.
For any vertex reachable from , the simple path in
the breadth-first tree from to corresponds to a
shortest path from to in G
that is, a path containing the smallest number of
edges.
The algorithm works on both directed and
undirected graphs.
BFS : How does it work?
it expands the frontier between
B discovered and undiscovered vertices
uniformly across the breadth of the
frontier.
A D
That is, the algorithm discovers all
vertices at distance k from s before
C discovering any vertices at distance
k+1.
BFS : How does it work?
To keep track of progress, breadth-first
B search colors each vertex white, gray,
or black.
A D All vertices start out white and may
later become gray and then black.

C A vertex is discovered the first time it is


encountered during the search, at
which time it becomes nonwhite.
BFS : How does it work?
Gray and black vertices, therefore,
B have been discovered,
but breadth-first search distinguishes
A D between them to ensure that
the search proceeds in a breadth-first
C manner.
BFS : How does it work?
If , and vertex is black, then
B vertex is either gray or black;
Gray vertices may have some
A D adjacent white vertices

C
BFS : Algorithm
Here
= is graph
a vertex in G
. color of
. distance of
. predecessor of
[] adjacent of
BFS : Algorithm
BFS : Algorithm
BFS : Analysis
Dequeuing take 1 and for vertices
Because the procedure scans the adjacency list of
each vertex only when the vertex is dequeued,
it scans each adjacency list at most once.
the total time spent in scanning adjacency list is ()
total running time of the BFS procedure is ( + )
Depth-first search (DFS)
The strategy is to search deeper in the graph whenever possible.

Depth-first search explores edges out of the most recently


discovered vertex that still has unexplored edges leaving it.

Once all of s edges have been explored, the search


backtracks to explore edges leaving the vertex from which was
discovered.

This process continues until we have discovered all the vertices that
are reachable from the original source vertex.
Depth-first search (DFS)

If any undiscovered vertices remain, then depth-first

search selects one of them as a new source, and it

repeats the search from that source.

The algorithm repeats this entire process until it has

discovered every vertex.


DFS : Algorithm
DFS : Algorithm
DFS
DFS : Analysis
total running time of the BFS procedure is ( + )
Topological Sort
Do you remember what is
Connected Graph
Cyclic Graph
Acyclic Graph
Topological Sort
We can only perform topological sort only on a
directed acyclic graph, or a dag.
Why?
Topological Sort
Definition
A

A topological sort of a dag = (, )


B
is a linear ordering of all its vertices
such that
if G contains an edge (, ) then
C appears before in the ordering.
Topological sort of this graph: , ,
Topological Sort
If there is a cycle
A

It is impossible to create a linier order


B
with that property.
So, we can not perform topological
sort on cyclic graph.
C
Topological Sort
Definition
A topological sort of a dag = (, ) is a linear
ordering of all its vertices such that
if G contains an edge (, ) then appears before
in the ordering.
(If the graph contains a cycle, then no linear
ordering is possible.)
Topological Sort
A

We can view a topological sort of a


B graph as an ordering of its vertices
along a horizontal line
so that all directed edges go from left
C
to right.
Topological sorting is thus different from
the usual kind of sorting.

A B C
Topological Sort
A

We can view a topological sort of a


B graph as an ordering of its vertices
along a horizontal line
so that all directed edges go from left
C
to right.
Topological sorting is thus different from
the usual kind of sorting.

A C B
Topological Sort
Use
Many applications use directed acyclic graphs to
indicate precedence among events.
Example
Professor Bumstead gets dressed in the morning.
Example
Professor Bumstead gets dressed in the morning.
Topological Sort
Example

http://www.shafaetsplanet.com/planetcoding/?p=973
Strongly Connected Components
Application of DFS
What is SCS?
Strongly Connected Components
Definition
A strongly connected component of a directed
graph = (, ) is a maximal set of vertices such
that
for every pair of vertices and in , we have both
and ;
that is, vertices and are reachable from each
other.
Strongly Connected Components
Strongly Connected Components
Spanning Tree
A spanning tree of a graph is a tree that has all the
vertices of the graph connected by some edges.
A graph can have one or more number of spanning
trees.
If the graph has N vertices then the spanning tree
will have N-1 edges.
Minimum Spanning Tree
A minimum spanning tree (MST) is a spanning tree
that has the minimum weight than all other spanning
trees of the graph.
Minimum Spanning Tree: Algorithms

Prims algorithm
Kruskals algorithm
Kruskals algorithm
Choose the shortest edge (if many choose any one)
Choose next shortest edge and add it
Choose next shortest edge which would not create
any cycle and add it
Repeat until you have a minimum spanning tree
Prims Algorithm
Choose a vector
Choose shortest path from this vector
Choose nearest vertex not yet in solution
Choose next nearest vertex not yet in solution, when
there is a choice chose either
Repeat until we have a minimum spanning tree
Complexity
Kruskal = ( log )
Prim = ( + )
Dijkstras algorithm