• No results found

CSC 162 DATA STRUCTURES

N/A
N/A
Protected

Academic year: 2021

Share "CSC 162 DATA STRUCTURES"

Copied!
54
0
0

Loading.... (view fulltext now)

Full text

(1)

CSC 162

(2)

Traversing graphs

Depth-First Search

like a post-order traversal of a tree

Breadth-First Search

(3)

Exploring a Maze

(4)

Example

A B C D

E F G H

I J K L

(5)

DFS

1. Start at vertex s

Tie the end of the string to s and mark “visited” on s Make s the current vertex u

2. Travel along an arbitrary edge (u,v)

unrolling string

3. If edge(u,v) leads to an already visited vertex v

then return to u

else mark v as “visited”, set v as current u, repeat @ step 2

4. When all edges lead to visited verticies, backtrack to

previous vertex (roll up string) and repeat @ step 2

5. When we backtrack to s and explore all it’s edges we

(6)

DFS Pseudocode (labels edges)

DFS( Vertex v)

for each edge incident on v do: if edge e is unexplored then

let w be the other endpoint of e if vertex w is unexplored then

label e as a discovery edge recursively call DFS(w)

else

(7)

Example

A B C D

E F G H

I J K L

(8)

Example

A B C D

E F G H

I J K L

(9)

Example

A B C D

E F G H

I J K L

(10)

Example

A B C D

E F G H

I J K L

(11)

Example

A B C D

E F G H

I J K L

(12)

Example

A B C D

E F G H

I J K L

(13)

Example

A B C D

E F G H

I J K L

(14)

Example

A B C D

E F G H

I J K L

(15)

Example

A B C D

E F G H

I J K L

(16)

Example

A B C D

E F G H

I J K L

(17)

Example

A B C D

E F G H

I J K L

(18)

Example

A B C D

E F G H

I J K L

(19)

Example

A B C D

E F G H

I J K L

(20)

Example

A B C D

E F G H

I J K L

(21)

Example

A B C D

E F G H

I J K L

(22)

Example

A B C D

E F G H

I J K L

(23)

Example

A B C D

E F G H

I J K L

(24)

Example

A B C D

E F G H

I J K L

(25)

Example

A B C D

E F G H

I J K L

(26)

Example

A B C D

E F G H

I J K L

(27)

Example

A B C D

E F G H

I J K L

(28)

Example

A B C D

E F G H

I J K L

(29)

Example

A B C D

E F G H

I J K L

(30)

DFS Tree

A B C D

E F G H

I J K L

(31)

DFS Properties

Starting at s

The traversal visits al the vertices in the connected component of s

(32)

DFS Runtime

DFS is called on each vertex exactly once

Every edge is examined exactly twice (once from each of its vertices)

So, for ns vertices and ms edges in the connected

component of the vertex s, the DFS runs in O(ns+ms) if:

- The graph data structure methods take constant time - Marking takes constant time

(33)

Marking Verticies

Extend vertex structure to support variable for marking

(34)

Breadth-First Search

Starting vertex has level 0 (anchor vertex)

Visit (mark) all vertices that are only one edge away

mark each vertex with its “level”

(35)

Example

A B C D

E F G H

I J K L

(36)
(37)
(38)
(39)
(40)
(41)
(42)
(43)
(44)
(45)
(46)
(47)
(48)
(49)
(50)
(51)

BFS Pseudocode (1 of 2)

BSF(Vertex s)

initialize container L0 to contain vertex s i  0

while Li is not empty do

create container Li+1 to initially be empty for each vertex v in Li do

(52)

BFS Pseudocode (2 of 2)

// for each vertex v in Li do if edge e incident on v do

let w be the other endpoint of e if w is unexplored then

label e as a discovery edge insert w into Li+1

else

(53)

BSF Properties

The traversal visits all vertices in the connected component of s

The discover edges form a spanning tree of the cc For each vertex v at level I, the path of the BSF

tree T between s and v has I edges and any other path of G between s and v has at least I edges

If (u,v) is an edge that is not in the BSF tree, then the level number of u and v differ by at most

(54)

Run Time

A BSF traversal takes O(n+m) time

Also, there exist O(n+m) time algorithms base on BFS which test for

Connectivity of graph Spanning tree of G

Connected component

References

Related documents

Established in 2000, e-VentExe is a full service Human Resource consulting company. The name e-VentExe stands for employees, Ventures, Executives. e-VentExe operates under the

Formally, this modification changes the definition of low(v) to the smallest starting time among all vertices in the same strong component as v that v can reach by a path of tree

The Ninth Circuit ultimately adopted the standard used by the Sixth and Tenth Circuits: "a defendant must show either: (1) that the restriction benefits the

By looking at the edges incident to a marked vertex v 0 and using Lemma 3.8, it follows that every net regular tree with marked vertex v 0 is constructed iteratively by adding

(5.28) Where: C V is the specific heat, v s is the speed of sound and l is the mean free path

• We will keep track of the parents of a vertex in P(v) then we can output either a shortest path using parents or a shortest path tree. • A shortest path tree is a subset of

 We will keep track of the parents of a vertex in P(v) then we can output either a shortest path using parents or a shortest path tree.  A shortest path tree is a subset of

In the M INIMUM B OUNDED D EGREE S PANNING T REE problem, we are given an undirected graph with a degree upper bound B v on each vertex v, and the task is to find a spanning tree