Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

08 Graph Algorithms Part1

This document discusses graph algorithms and concepts. It defines what a graph is and different graph terminology like vertices, edges, paths, cycles, connectivity, and representations. It then describes two common graph traversal algorithms - breadth-first search and depth-first search. Breadth-first search is defined as starting at a source vertex and visiting all neighbors first before moving to the next level. Depth-first search prioritizes going deeper before exploring neighbors.

Uploaded by

Ahmad Alaraby
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

08 Graph Algorithms Part1

This document discusses graph algorithms and concepts. It defines what a graph is and different graph terminology like vertices, edges, paths, cycles, connectivity, and representations. It then describes two common graph traversal algorithms - breadth-first search and depth-first search. Breadth-first search is defined as starting at a source vertex and visiting all neighbors first before moving to the next level. Depth-first search prioritizes going deeper before exploring neighbors.

Uploaded by

Ahmad Alaraby
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 76

Algorithms

Graph Algorithms-Part1
Computer Science Dept.
Instructor: Dr. Ameera Jaradat
Outline
 Concepts & Terminology
 Representation
 Traversal
 Breadth-first Search (BFS).
 Depth-first Search (DFS).

2
?What is a Graph
 A graph G = (V, E) consists of:
 V: set of vertices
 E: set of edges connecting the vertices in V
 An edge e = (v, w) is a pair of vertices, where v, w  V. Sometimes each
edge is associated with a weight or a cost.
 If the pair is ordered, then the graph is directed. Directed graphs are
sometimes referred to as digraphs.
 Examples
a b a b

c c

d e d e

V = {a, b, c, d, e} V = {a, b, c, d, e}
,E = {(a, b), (a, c), (a, d) ,E = {(a, b), (a, c), (a, d)
,)c, e( ,)c, d( ,)b, e( ,)c, e( ,)c, d( ,)b, e(
})d, e( )pairs are ordered( })d, e(
Problems that can be represented by a graph

 computer networks
 airline flights
 road map
 course prerequisite structure
 many more
 Any individuals interaction can be represented as a graph were the
individuals are the nodes and the interactions are the links.
Graph Terminology
 Path
 A path in a graph is a sequence of vertices w1, w2, w3, …, wN such that
wi+1 is adjacent to wi (i.e, (wi, wi+1)  E) for 1 <= i < N.
a b a b

c c

d e d e

.abedce is a path .acde is a path


.cdeb is a path .abec is NOT a path
.bca is NOT a path

 The length of a path, w1, w2, w3, …, wN, is the number of edges on the
path, which is equal to N-1.
Graph Terminology
 Loops
 If the graph contains an edge (v, v) from a vertex to itself, then the path
v, v is sometimes referred to as a loop.
a b

d e

 Simple paths
 A simple path is a path such that all vertices are distinct, except that the
first and last could be the same.
a b
abedc is a simple path.
c cdec is a simple path.
abedce is NOT a simple path.
d e
Graph Terminology
 Cycles
 A cycle in a directed graph is a path of length at least 1 such that the first
vertex on the path is the same as the last one; if the path is simple, then
the cycle is a simple cycle.
a b
.abeda is a simple cycle
c .abeceda is a cycle, but is NOT a simple cycle
.abedc is NOT a cycle
d e

 A cycle in a undirected graph


 A path of length at least 1 such that the first vertex on the path is the same as
the last one.
 a
The edges b are distinct.
on the path
.aba is NOT a cycle
.abedceda is NOT a cycle
c
.abedcea is a cycle
d e
Graph Terminology
 DAG (Directed acyclic graph)
 A directed graph is acyclic if it has no cycles.

a b
a b

c
c
d e
d e

Not a DAG A DAG


Graph Terminology
 Connectivity
 In an undirected graph G, two vertices u and v are called
connected if G contains a path from u to v. Otherwise, they
are called disconnected.
 If the two vertices are additionally connected by a path of
length 1, i.e. by a single edge, the vertices are called
adjacent.
 A graph is said to be connected if every pair of vertices in
the graph is connected.
a b a b

c c

d e d e
Connectivity in Directed Graphs
Weakly connected : can get from a to b in underlying undirected graph

Strongly connected : can get from a to b AND from b to a in the digraph


DEF: A graph is strongly connected if every pair of vertices is connected in
the same sense.
If a directed graph is not strongly connected, but the underlying graph
(without direction to the arcs) is connected, then the graph is said to be weakly
connected

weak strong
Degree of a Vertex

• The degree of a vertex is the number of edges


incident to that vertex
• For directed graph,
– the in-degree of a vertex v is the number of edges
that have v as the head
– the out-degree of a vertex v is the number of edges
that have v as the tail
Complete Graph
 Denoted Kn
 Every pair of vertices are adjacent
 Directed complete has n(n-1) edges
 Undirected complete has n(n-1)/2 edges

a b

d c
Complete Graph
 Let n = #vertices, and m = #edges
 A complete graph: all pairs of vertices are adjacent
 How many total edges in a complete graph?
 Each of the n vertices is incident to n-1 edges, however, we would have
counted each edge twice! Therefore, intuitively, m = n(n -1)/2.
 Therefore, if a graph is not complete, 0 < m < n(n -1)/2
 For a connected graph of n vertices:
n ( n  1)
n 1 m 
2
n 5
tree complete m  (5 
Weighted vs unweighted
Sparse vs Dense
 A graph is sparse if | E |  | V |
 A graph is dense if | E |  | V |2.
Simple vs not simple graph

 Simple Graph : A graph with no parallel edges or self


loops.
1 1 2
2

Not simple simple

4 4 3
3


Representation of Graphs
 Adjacency matrix representation
 Adjacency list representation
Adjacency Matrix
 |V|  |V| matrix A.
 Number vertices from 1 to |V| in some arbitrary manner.
 A is then given by: A[i, j ]  a  1 if (i, j )  E
ij 
0 otherwise
1 2
a b 4 3 2 1
1 1 1 0 1
0 1 0 0 2
c d4 1 0 0 0 3
3 0 0 0 0 4
1 2 4 3 2 1 .A = AT for undirected graphs
a b
1 1 1 0 1
0 1 0 1 2
c d 1 0 1 1 3
3 4 0 1 0 1 4
Adjacency Lists
 Consists of an array Adj of |V| lists.
 One list per vertex.
 For u  V, Adj[u] consists of all vertices adjacent to u.
a b a b d c

b c

c d c d
If weighted, store weights also in
d adjacency lists.

a b a b d c

b a c

c d c d a b

d a c
Storage Requirement
 For directed graphs:
 Sum of lengths of all adj. lists is
out-degree(v) = |E|
vV No. of edges leaving v
 Total storage: (V+E)
 For undirected graphs:
 Sum of lengths of all adj. lists is
degree(v) = 2|E|
vV
No. of edges incident on v. Edge (u,v) is incident
on vertices u and v.
 Total storage: (V+E)
List vs. Matrix
 Adjacency list representation
 Space-efficient, when a graph is sparse.
 Determining if an edge (u,v) G is not efficient.
 Search in u’s adjacency list. (degree(u)) time.
 (V) in the worst case.
 Adjacency matrix representation
 Space: (V2).
 An appropriate representation for a dense graph.
 list all vertices adjacent to u: (V).
 Determine if (u, v)  E: (1).
Graph-searching Algorithms
 Searching a graph:
 Systematically follow the edges of a graph to visit the
vertices of the graph.
 Used to discover the structure of a graph.
 Standard graph-searching algorithms.
 Breadth-first Search (BFS).
 Depth-first Search (DFS).
Breadth-first Search

 Input: Graph G = (V, E), either directed or undirected,


and source vertex s  V.
 Output:
 d[v] = distance (smallest # of edges, or shortest path) from s to
v, for all v  V. d[v] =  if v is not reachable from s.
 [v] = u such that (u, v) is last edge on shortest path s v.
 u is v’s predecessor.
 Builds breadth-first tree with root s that contains all reachable
vertices.
Breadth-first Search
 Expands the frontier between discovered and
undiscovered vertices uniformly across the breadth of the
frontier.
 A vertex is “discovered” the first time it is encountered during the
search.
 A vertex is “finished” if all vertices adjacent to it have been
discovered.
 Colors the vertices to keep track of progress.
 White – Undiscovered.
 Gray – Discovered but not finished.
 Black – Finished.
 Colors are required only to reason about the algorithm. Can be implemented
without colors.
BFS(G,s)
1. for each vertex u in V[G] – {s}
2 do color[u]  white
3 d[u]  
4 [u]  nil white: undiscovered
gray: discovered
5 color[s]  gray black: finished
6 d[s]  0
7 [s]  nil
Q: a queue of discovered
8 Q
vertices
9 enqueue(Q,s) color[v]: color of v
10 while Q   d[v]: distance from s to v
11 do u  dequeue(Q) [u]: predecessor of v

12 for each v in Adj[u]


13 do if color[v] = white Example: animation.
14 then color[v]  gray
15 d[v]  d[u] + 1
16 [v]  u
17 enqueue(Q, v)
18 color[u]  black
Example (BFS)

r s t u
 0  

   
v w x y

Q: s
0

)Courtesy of Prof. Jim Anderson(


Example (BFS)

r s t u
1 0  

 1  
v w x y

Q: w r
1 1
Example (BFS)

r s t u
1 0 2 

 1 2 
v w x y

Q: r t x
2 2 1
Example (BFS)

r s t u
1 0 2 

2 1 2 
v w x y

Q: t x v
2 2 2
Example (BFS)

r s t u
1 0 2 3

2 1 2 
v w x y

Q: x v u
3 2 2
Example (BFS)

r s t u
1 0 2 3

2 1 2 3
v w x y

Q: v u y
3 3 2
Example (BFS)

r s t u
1 0 2 3

2 1 2 3
v w x y

Q: u y
3 3
Example (BFS)

r s t u
1 0 2 3

2 1 2 3
v w x y

Q: y
3
Example (BFS)

r s t u
1 0 2 3

2 1 2 3
v w x y

Q: 
Example (BFS)

r s t u
1 0 2 3

2 1 2 3
v w x y

BF Tree
Analysis of BFS
 Initialization takes O(V).
 Traversal Loop
 After initialization, each vertex is enqueued and dequeued at most
once, and each operation takes O(1). So, total time for queuing is
O(V).
 The adjacency list of each vertex is scanned at most once. The
sum of lengths of all adjacency lists is (E).

 Summing up over all vertices => total running time of BFS


is O(V+E)
Depth-first Search (DFS)
 Explore edges out of the most recently discovered vertex v.
 When all edges of v have been explored, backtrack to explore
other edges leaving the vertex from which v was discovered (its
predecessor).
 “Search as deep as possible first.”
 Continue until all vertices reachable from the original source are
discovered.
 If any undiscovered vertices remain, then one of them is chosen
as a new source and search is repeated from that source.
Depth-first Search
 Input: G = (V, E), directed or undirected. No source
vertex given!
 Output:
 2 timestamps on each vertex. Integers between 1 and 2|V|.
 d[v] = discovery time (v turns from white to gray)
 f [v] = finishing time (v turns from gray to black)
 [v] : predecessor of v = u, such that v was discovered during the
scan of u’s adjacency list.
 Uses the same coloring scheme for vertices as BFS.
Pseudo-code
DFS(G) DFS-Visit(u)
1. for each vertex u  V[G] 1. color[u]  GRAY  White vertex u has
2. do color[u]  white been discovered
3. [u]  NIL 2. time  time + 1
3. d[u]  time
4. time  0
4. for each v  Adj[u]
5. for each vertex u  V[G]
5. do if color[v] = WHITE
6. do if color[u] = white
6. then [v]  u
7. then DFS-Visit(u) 7. DFS-Visit(v)
8. color[u]  BLACK  Blacken u; it is
finished.
9. f[u]  time  time + 1
Example (DFS)

u v w
/1

x y z
Example (DFS)

u v w
/1 /2

x y z
Example (DFS)

u v w
/1 /2

/3
x y z
Example (DFS)

u v w
/1 /2

/4 /3
x y z
Example (DFS)

u v w
/1 /2

/4 /3
x y z
Example (DFS)

u v w
/1 /2

4/5 /3
x y z
Example (DFS)

u v w
/1 /2

4/5 3/6
x y z
Example (DFS)

u v w
/1 2/7

4/5 3/6
x y z
Example (DFS)

u v w
/1 2/7

F B

4/5 3/6
x y z
Example (DFS)

u v w
1/8 2/7

F B

4/5 3/6
x y z
Example (DFS)

u v w
1/8 2/7 /9

F B

4/5 3/6
x y z
Example (DFS)

u v w
1/8 2/7 /9

F B C

4/5 3/6
x y z
Example (DFS)

u v w
1/8 2/7 /9

F B C

4/5 3/6 /10


x y z
Example (DFS)

u v w
1/8 2/7 /9

F B C

4/5 3/6 /10 B


x y z
Example (DFS)

u v w
1/8 2/7 /9

F B C

4/5 3/6 10/11 B


x y z
Example (DFS)

u v w
1/8 2/7 9/12

F B C

4/5 3/6 10/11 B


x y z
Analysis of DFS
 Loops on lines 1-2 & 5-7 take (V) time, excluding time
to execute DFS-Visit.
 DFS-Visit is called once for each white vertex vV when
it’s painted gray the first time. Lines 3-6 of DFS-Visit is
executed |Adj[v]| times. The total cost of executing DFS-
Visit is vV|Adj[v]| = (E)

 Total running time of DFS is (V+E).


Example (DFS)
After a DFS of graph G we can put each
edge into one of four classes:
1. tree edge is an edge in a DFS-tree.
u v w
2. back edge connects a vertex to an
ancestor in a DFS-tree. 1/8 2/7 9/12
3. forward edge is a non-tree edge that
connects a vertex to a descendent in B C
F
a DFS-tree.
4. cross edge is any other edge in graph
G. It connects vertices in two B
4/5 3/6 10/11
different DFS-tree or two vertices in
the same DFS-tree neither of which x y z
is the ancestor of the other.
Edge classification by DFS

Tree edges
Forward edges
Back edges a

Cross edges
b c

c
Applications of DFS(1)

 Cycle detection:
 Does a given graph G contain a cycle?

 Thm: If G is undirected, a DFS produces only tree and


back edges

 Thm: A graph G (directed or not) contains a cycle if and


only if a DFS of G yields a back edge.

60
DFS And Cycles
 A digraph is acyclic if and only if any DFS forest of yields
no back edges.
 What will be the running time? O(V+E)
 We can actually determine if cycles exist in O(V) time:
 In an undirected acyclic forest, |E|  |V| - 1
 So count the edges: if ever see |V| distinct edges, must have
seen a back edge along the way
Applications of DFS(2)
 Connected components of an undirected graph.
 Each call to DFS_VISIT (from DFS) explores an entire connected
component (see DFS(G) line 4).
 modify DFS to count the number of times it calls
DFS_VISIT
4. for each vertex u  G->V
5. if (u->color == WHITE)
6. cc_counter ←cc_counter + 1
7. DFS_Visit(u);

62
Applications of DFS(3)
 Topological sort
 if G = (V, E) is a DAG then a topological sorting of G
is a linear ordering of G such that for each edge (u, v)
in the DAG, u appears before v in the linear ordering.

 Such an ordering is called a topological sort of G


Algorithm for TS
 Run DFS(G), computing finish time for each vertex;
 As each vertex is finished, insert it onto the front of a list;

 TOPOLOGICAL-SORT(G):
1) call DFS(G) to compute finishing times f[v] for each vertex
v
2) as each vertex is finished, insert it onto the front of a linked
list
3) return the linked list of vertices
Topological sort

∞=d Let’s say we start the DFS


∞=f a from the vertex c

∞=d ∞ d
d=1
∞=f b c ∞=f

∞=d ∞=d
∞=f d e ∞=f

∞=d f
∞=f
Topological sort

∞=d
∞=f a

∞=d d=1
∞=f b c ∞=f


d=2
d ∞=d
∞=f d e ∞=f

∞=d f
∞=f
Topological sort

∞=d
∞=f a

∞=d d=1
∞=f b c ∞=f

d=2 ∞=d
∞=f d e ∞=f

d=3 f

f ==4f

f
Topological sort

∞=d
∞=f a

∞=d d=1
∞=f b c ∞=f

d=2 ∞=d
f=5 d e ∞=f

d=3 f
f=4

d f
Topological sort

∞=d
∞=f a

∞=d d=1
∞=f b c ∞=f

d=2 ∞=d
f=5 d e ∞=f

d=3 f
f=4

d f
Topological sort

∞=d
∞=f a

∞=d d=1
∞=f b c ∞=f

d=2 d=6
f=5 d e ∞=f

d=3 f
f=4

e d f
Topological sort

∞=d
∞=f a

∞=d d=1
∞=f b c ∞=f

d=2 d=6
f=5 d e f=7

d=3 f
f=4

c e d f
Topological sort

∞ d
d=9
∞=f a

∞=d d=1
∞=f b c f=8

d=2 d=6
f=5 d e f=7

d=3 f
f=4

c e d f
Topological sort

d=9
∞=f a

d = 10 d=1
11f b
f∞= = c f=8

d=2 d=6
f=5 d e f=7

d=3 f
f=4

b c e d f
Topological sort

d=9
∞=f a

d = 10 d=1
f = 11 b c f=8

d=2 d=6
f=5 d e f=7

d=3 f
f=4

b c e d f
Topological sort

d=9
f = 12 a

d = 10 d=1
f = 11 b c f=8

d=2 d=6
f=5 d e f=7

d=3 f
f=4

a b c e d f
Topological sort

d=9
f = 12 a

d = 10 d=1
f = 11 b c f=8

d=2 d=6
d e
f=5 f=7

d=3 f
f=4

a b c e d f
Time complexity of TS(G)
 Running time of topological sort:
Θ(n + m)
where n=|V| and m=|E|
 Why? Depth first search takes Θ(n + m) time in the worst
case, and inserting into the front of a linked list takes Θ(1)
time

You might also like