Lecture topic-BruteForce

Createstack spushv count count 1 mark v with count

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: lgorithm Algorithm dfs(v) s.createStack(); s.push(v); count := count + 1 mark v with count while (!s.isEmpty()) { let x be the node on the top of the stack s; if (no unvisited nodes are adjacent to x) s.pop(); // backtrack else { select an unvisited node u adjacent to x; s.push(u); count := count + 1 mark u with count } Design and Analysis of Algorithms – Chapter 4 } 10 Time efficiency analysis a DFS can be implemented with graphs represented as: • Adjacency matrices: DFS(G) count :=0 mark each vertex with 0 mark (unvisited) (unvisited) for each vertex v∈ V do do if v is marked with 0 if dfs(v) dfs(v) dfs(v) count := count + 1 mark v with count mark for each vertex w adjacent to for v do do if w is marked with 0 if dfs(w) dfs( Design and Analysis of Algorithms – Chapter 4 11 Time efficiency analysis a DFS can be implemented with graphs represented as: • Adjacency linked lists: DFS(G) count :=0 mark each vertex with 0 mark (unvisited) (unvisited) for each vertex v∈ V do do if v is marked with 0 if dfs(v) dfs(v) dfs(v) count := count + 1 mark v with count mark for each vertex w adjacent to for v do do if w is marked with 0 if dfs(w) dfs( Design and Analysis of Algorithms – Chapter 4 12 Time efficiency analysis a DFS can be implemented with graphs represented as: • Adjacency matrices: Θ(V2) Adjacency • Adjacency linked lists: Θ(V+E) Adjacency +E) DFS(G) count :=0 mark each vertex with 0 mark (unvisited) (unvisited) for each vertex v∈ V do do if v is marked...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online