Graphs - PowerPoint PPT Presentation

1 / 74
About This Presentation
Title:

Graphs

Description:

Graphs & Paths Presentation : Part II Graph representation Given graph G = (V, E). May be either directed or undirected. Two common ways to represent for algorithms ... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 75
Provided by: SAVS
Learn more at: https://www.cs.gsu.edu
Category:
Tags: backtrack | graphs

less

Transcript and Presenter's Notes

Title: Graphs


1
Graphs Paths
  • Presentation Part II

2
Graph representation
  • Given graph G (V, E).
  • May be either directed or undirected.
  • Two common ways to represent for algorithms
  • Adjacency lists.
  • Adjacency matrix.
  • When expressing the running time of an algorithm,
    its often in terms of both V and E.

3
Adjacency lists
  • Array Adj of V lists, one per vertex.
  • Vertex us list has all vertices v such that (u,
    v) ? E. (Works for both directed and undirected
    graphs.)

4
Example
  • For an undirected graph

5
Example
  • For a directed graph

Same asymptotic space and time.
6
Adjacency matrix
7
Adjacency matrix
8
Breadth-first search
  • Input Graph G (V, E), either directed or
    undirected, and source vertex s ? V.
  • Output dv distance (smallest of edges)
    from s to v, for all v ? V.

9
Idea
  • Idea Send a wave out from s.
  • First hits all vertices 1 edge from s.
  • From there, hits all vertices 2 edges from s.
  • Etc.
  • Use FIFO queue Q to maintain wavefront.
  • v ? Q if and only if wave has hit v but has not
    come out of v yet.

10
Breadth-First Search
11
Breadth-First Search
r
s
t
u
x
v
w
y
12
Breadth-First Search
r
s
t
u
x
v
w
y
Q
s
0
13
Breadth-First Search
r
s
t
u
x
v
w
y
Q
r
w
1
1
14
Breadth-First Search
r
s
t
u
x
v
w
y
Q
w
v
1
2
15
Breadth-First Search
r
s
t
u
x
v
w
y
Q
v
t
x
2
2
2
16
Breadth-First Search
r
s
t
u
x
v
w
y
Q
t
x
2
2
17
Breadth-First Search
r
s
t
u
x
v
w
y
Q
x
u
2
3
18
Breadth-First Search
r
s
t
u
x
v
w
y
Q
u
y
3
3
19
Breadth-First Search
r
s
t
u
x
v
w
y
Q
y
3
20
Breadth-First Search
r
s
t
u
x
v
w
y
Q
?
21
Breadth-First Search
  • Run-time O(VE)
  • Shortest-path tree
  • the final weight is the minimum distance
  • keep predecessors and get the shortest path
  • all BFS shortest paths make a tree

22
Breadth-First SearchExample
23
Breadth-First Search
  • Since each vertex gets a finite d value at most
    once, values assigned to vertices are
    monotonically increasing over time.
  • Time O(V E).
  • O(V) because every vertex enqueued at most once.
  • O(E) because every vertex dequeued at most once
    and we examine (u, v) is in E only when u is
    dequeued. Therefore, every edge examined at most
    once if directed, at most twice if undirected.

24
Depth-first search
  • Input G (V, E), directed or undirected. No
    source vertex given!
  • Output 2 timestamps on each vertex
  • dv discovery time
  • f v finishing time

25
Depth-first search
  • Will methodically explore every edge.
  • Start over from different vertices as necessary.
  • As soon as we discover a vertex, explore from it.
  • Unlike BFS, which puts a vertex on a queue so
    that we explore from it later.
  • As DFS progresses, every vertex has a color
  • WHITE undiscovered
  • GRAY discovered, but not finished (not done
    exploring from it)
  • BLACK finished (have found everything reachable
    from it)
  • Discovery and finish times Unique integers from
    1 to 2V.
  • For all v, dv lt f v.
  • In other words, 1 dv lt f v 2V.

26
(No Transcript)
27
Depth-First Search
  • Methodically explore all vertices and edges
  • All vertices are White, t 0
  • For each vertex u do if u is White then
    Visit(u)
  • Procedure Visit(u)
  • color u Gray
  • du ? t ? t 1
  • for each v adjacent to u do
  • if v is White then Visit(v)
  • color u Black
  • f u ? t ? t 1
  • Gray vertices stack of recursive calls

28
Depth-First Search
29
Depth-First Search
1
30
Depth-First Search
1
2
2
31
Depth-First Search
1
3
2
2
32
Depth-First Search
1
3
2
2
33
Depth-First Search
1
3
2
2
34
Depth-First Search
1
3
2
35
Depth-First Search
1
4
3
2
36
Depth-First Search
1
4
3
2
37
Depth-First Search
1
4
3
2
38
Depth-First Search
5
1
4
3
2
39
Depth-First Search
5
1
4
3
2
40
Depth-First Search
5
1
4
6
3
2
41
Depth-First Search
5
1
4
6
3
2
42
Depth-First Search
5
1
4
6
3
2
43
Depth-First Search
5
1
4
6
3
2
44
Depth-First Search
5
1
4
6
3
2
45
Depth-First Search
5
7
1
4
6
3
2
46
Depth-First Search
5
7
1
4
6
3
2
47
Depth-First Search
5
7
1
4
6
3
8
2
48
Depth-First Search
5
7
1
4
6
3
8
2
49
Depth-First Search
5
7
1
4
6
3
8
2
50
Depth-First Search
5
7
1
4
6
3
8
2
51
Depth-First Search
5
7
1
4
6
3
8
2
52
Review Depth-First Search
  • Depth-first search is another strategy for
    exploring a graph
  • Explore deeper in the graph whenever possible.
  • Edges are explored out of the most recently
    discovered vertex v that still has unexplored
    edges.
  • When all of vs edges have been explored,
    backtrack to the vertex from which v was
    discovered

53
DFS Applications.
  • Testing whether graph is connected.
  • Computing a spanning forest of graph.
  • Computing a path between two vertices of graph or
    equivalently reporting that no such path exists.
  • Computing a cycle in graph or equivalently
    reporting that no such cycle exists.

54
DFS vs. BFS
  • DFS seems cleaner than BFS recursive, it doesn't
    make use of an external data structure (a queue),
    and is shorter too.
  • But it is important to realize these two
    algorithms address very different needs.
  • An example of BFS usage is calculating shortest
    paths.
  • A Depth First Search performed on the World Wide
    Web would quickly overload any given web server
    with an ever growing number of requests.
  • Therefore a Breadth First Search is preferred,
    since it dispatches requests to many servers at a
    time.

55
DFS vs. BFS
Applications DFS BFS
Spanning forest, connected components, paths, cycles ? ?
Shortest paths ?
Biconnected components ?
56
Example 2
57
Example 2 animated
sourcevertex
58
DFS Example
sourcevertex
d f
1







59
DFS Example
sourcevertex
d f
1


2




60
DFS Example
sourcevertex
d f
1


2



3
61
DFS Example
sourcevertex
d f
1


2



3 4
62
DFS Example
sourcevertex
d f
1


2


5
3 4
63
DFS Example
sourcevertex
d f
1


2


5 6
3 4
64
DFS Example
sourcevertex
d f
1
8

2 7


5 6
3 4
65
DFS Example
sourcevertex
d f
1
8

2 7


5 6
3 4
66
DFS Example
sourcevertex
d f
1
8

2 7
9

5 6
3 4
What is the structure of the yellow vertices?
What do they represent?
67
DFS Example
sourcevertex
d f
1
8

2 7
9 10

5 6
3 4
68
DFS Example
sourcevertex
d f
1
8 11

2 7
9 10

5 6
3 4
69
DFS Example
sourcevertex
d f
1 12
8 11

2 7
9 10

5 6
3 4
70
DFS Example
sourcevertex
d f
1 12
8 11
13
2 7
9 10

5 6
3 4
71
DFS Example
sourcevertex
d f
1 12
8 11
13
2 7
9 10
14
5 6
3 4
72
DFS Example
sourcevertex
d f
1 12
8 11
13
2 7
9 10
1415
5 6
3 4
73
DFS Example
sourcevertex
d f
1 12
8 11
1316
2 7
9 10
1415
5 6
3 4
74
Snapshot
Write a Comment
User Comments (0)
About PowerShow.com