DAST 2005 Tirgul 10 AVL Trees Graphs DFS sample questions.

Slides:



Advertisements
Similar presentations
Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Advertisements

Comp 122, Fall 2004 Elementary Graph Algorithms. graphs Lin / Devi Comp 122, Fall 2004 Graphs  Graph G = (V, E) »V = set of vertices »E = set of.
Tirgul 7 Review of graphs Graph algorithms: –DFS –Properties of DFS –Topological sort.
Elementary Graph Algorithms Depth-first search.Topological Sort. Strongly connected components. Chapter 22 CLRS.
CS 473Lecture 141 CS473-Algorithms I Lecture 15 Graph Searching: Depth-First Search and Topological Sort.
David Luebke 1 5/9/2015 CS 332: Algorithms Graph Algorithms.
Graphs Breadth First Search & Depth First Search by Shailendra Upadhye.
Graphs Searching. Graph Searching Given: a graph G = (V, E), directed or undirected Goal: methodically explore every vertex and every edge Ultimately:
CS 3343: Analysis of Algorithms Lecture 24: Graph searching, Topological sort.
1 Graph Programming Gordon College. 2 Graph Basics A graph G = (V, E) –V = set of vertices, E = set of edges –Dense graph: |E|  |V| 2 ; Sparse graph:
Graph Traversals Visit vertices of a graph G to determine some property: Is G connected? Is there a path from vertex a to vertex b? Does G have a cycle?
Graph traversals / cutler1 Graph traversals Breadth first search Depth first search.
Tirgul 8 Graph algorithms: Strongly connected components.
Tirgul 11 DFS Properties of DFS Topological sort.
Graphs-Part II Depth First Search (DFS). We Already Covered Breadth First Search(BFS) Traverses the graph one level at a time – Visit all outgoing edges.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
David Luebke 1 5/20/2015 CS 332: Algorithms Graph Algorithms.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Discussion #36 Spanning Trees
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2001 Makeup Lecture Chapter 23: Graph Algorithms Depth-First SearchBreadth-First.
Shortest Path Problems
1 Data Structures DFS, Topological Sort Dana Shapira.
CSE 780 Algorithms Advanced Algorithms Graph Alg. DFS Topological sort.
DAST 2005 Tirgul 11 (and more) sample questions. DAST 2005 Q.Let G = (V,E) be an undirected, connected graph with an edge weight function w : E→R. Let.
1 7/2/2015 ITCS 6114 Graph Algorithms. 2 7/2/2015 Graphs ● A graph G = (V, E) ■ V = set of vertices ■ E = set of edges = subset of V  V ■ Thus |E| =
1 7/3/2015 ITCS 6114 Graph Algorithms. 2 7/3/2015 Depth-First Search ● Depth-first search is another strategy for exploring a graph ■ Explore “deeper”
DAST 2005 Tirgul 9 Binary Search Trees sample questions.
David Luebke 1 8/7/2015 CS 332: Algorithms Graph Algorithms.
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
David Luebke 1 9/15/2015 CS 332: Algorithms Topological Sort Minimum Spanning Trees.
David Luebke 1 10/1/2015 CS 332: Algorithms Topological Sort Minimum Spanning Tree.
Spring 2015 Lecture 10: Elementary Graph Algorithms
Sept Elementary Graph Algorithms Graph representation Graph traversal -Breadth-first search -Depth-first search Parenthesis theorem.
 2004 SDU Lecture 7- Minimum Spanning Tree-- Extension 1.Properties of Minimum Spanning Tree 2.Secondary Minimum Spanning Tree 3.Bottleneck.
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
CSC 413/513: Intro to Algorithms Graph Algorithms DFS.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Jan Topological Order and SCC Edge classification Topological order Recognition of strongly connected components.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
 2004 SDU Lectrue4-Properties of DFS Properties of DFS Classification of edges Topological sort.
CSC 201: Design and Analysis of Algorithms Lecture # 18 Graph Algorithms Mudasser Naseer 1 12/16/2015.
CSC 413/513: Intro to Algorithms Graph Algorithms.
Graph Algorithms Searching. Review: Graphs ● A graph G = (V, E) ■ V = set of vertices, E = set of edges ■ Dense graph: |E|  |V| 2 ; Sparse graph: |E|
Chapter 22: Elementary Graph Algorithms
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
David Luebke 1 1/6/2016 CS 332: Algorithms Graph Algorithms.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
CS 2133: Algorithms Intro to Graph Algorithms (Slides created by David Luebke)
David Luebke 1 1/25/2016 CSE 207: Algorithms Graph Algorithms.
Liaquat Majeed Sheikh 1 1/25/2016 Graph Algorithms.
Shahed University Dr. Shahriar Bijani May  A path is a sequence of vertices P = (v 0, v 1, …, v k ) such that, for 1 ≤ i ≤ k, edge (v i – 1, v.
Chapter 05 Introduction to Graph And Search Algorithms.
DAST Tirgul 6. What is a Binary Search Tree? The keys in a binary search tree (BST) are always stored in such a way as to satisfy the search property:
Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.
CS 3343: Analysis of Algorithms Lecture 24: Graph searching, Topological sort.
64 Algorithms analysis and design BY Lecturer: Aisha Dawood.
DAST Tirgul 7.
Chapter 22 Elementary Graph Algorithms
Topological Sort Minimum Spanning Tree
Depth-First Search Depth-first search is a strategy for exploring a graph Explore “deeper” in the graph whenever possible Edges are explored out of the.
Data Structures Review Session 2
CSC 413/513: Intro to Algorithms
Many slides here are based on E. Demaine , D. Luebke slides
Elementary Graph Algorithms
CS 3343: Analysis of Algorithms
Graphs A graph G = (V, E) V = set of vertices, E = set of edges
Intro to Graph Algorithms (Slides originally created by David Luebke)
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
CSC 325: Algorithms Graph Algorithms David Luebke /24/2019.
Presentation transcript:

DAST 2005 Tirgul 10 AVL Trees Graphs DFS sample questions

DAST 2005 Q.We have two BSTs, A having n nodes and B having m nodes, where n ≥ m. We would like to find their intersection (values that appear in both trees) so we decide to perform an inorder traversal of A and search for each value in B. What is the worst-case running time of our approach? What if we traverse BST B and search for the values in A? A.We traverse through all the members of A (n members). For each of these members we perform a search in B. Since the running time of search is proportional to the height of the tree B which is m in the worst case the total running time is O(n*m). Traversing through B and searching for the members of A yields the same running time

DAST 2005 Q.This time A and B are AVL trees. What is the worst-case running time of our approach? What if we traverse B and search for the values in A? A.We traverse through all the members of A (n members). For each of these members we perform a search in B. Since the running time of search is proportional to the height of the tree B which is O(log(m)) in the worst case  The total running time is O(n*log(m)). Traversing through B and searching for the members of A yields a running time of O(m*log(n)) (which is better if n ≥ m).

DAST 2005 Q.Can you think of a better approach? A.We dump the trees into lists (inorder traversal: O(n) + O(m)) and find the intersection between the lists. Finding the intersection between two sorted lists is linear in the number of elements in both lists: List listsIntersection(L1,L2) { L3 = new List; while(L1 != NULL && L2 != NULL) { if (L1.data ≤ L2.data) { if (L1.data == L2.data) { L3.add(L1.data); L2 = L2.next; } L1 = L1.next;} else { L2 = L2.next;}} return L3; } in each iteration we advance at least one of the pointers: The total number of iterations ≤ n+m

DAST 2005 Q.We have two AVL trees of height h such that all elements in A are smaller than all elements in B, write an efficient algorithm for building an AVL tree C containing all the members of A and B, what is its complexity? A.Remove the maximum of A (O(h)), use it as a root for C (A is the left subtree, while B is the right subtree) Q.What if A is higher than B by 2? A.We can still merge the trees efficiently like in the previous case but we might have to fix C using rotations.

DAST 2005 DFS – pseudo code DFS(G) //initializing. for each vertex u  V[G] { u.color = white; u.prev = nil; } time = 0; for each vertex u  V[G] { if (u.color == white) DFS-VISIT(u) }

DAST 2005 DFS – pseudo code (cont.) DFS-VISIT(u) u.color = gray; u.d = ++time; for each vertex v  adj[u] { if (v.color == white) { v.prev = u; DFS-VISIT(v); } u.color = black; u.f = ++time;

DAST 2005 Q.Let G be a graph having |V| vertices and |E| edges What is the asymptotic time complexity of running DFS on G when G is represented using: (a) an adjacency list? (b) an adjacency matrix? When will the asymptotic time complexity be identical? When will we have the largest difference? A.The first stage goes through all the vertices (O(|V|) regardless of the representation) In the second stage we have a constant amount of work for each potential edge, which is O(|E|) in case of an adjacency list and O(|V 2 |) in case of an adjacency matrix. In full graphs O(|E|) == O(|V 2 |), the sparser the graph, the bigger the difference (in a tree O(|E|) == O(|V|))

DAST 2005 Q.Apply DFS to the following graph. Whenever the algorithm has a choice, assume that the node that comes earlier in the alphabet is considered first (like B before E). (This makes the algorithm deterministic, so you can demonstrate that you understand it fully.) Give the resulting DFS-tree(s) for the graph. BCAEFD

DAST 2005 A. BCAEFDBCAEFD

DAST 2005 Q.Is it true that in an undirected graph G, if there is a path between two vertices u and v then in the DFS-tree(s) of G, either v is a sibling of u or u is a sibling of v. Proof or give a counter example A.False (in the previous graph there is a path from C to F yet they are not siblings of one another)

DAST 2005 Q.Suggest an algorithm (pseudo-code) for labeling (with labels 1,2,…) the connected components in an undirected graph (all vertices in the same connected component should have the same label). What is it’s complexity? A.This is a very small modification of the DFS algorithm. Instead of coloring each finished node in black we color it with a label. We increase the label for each connected component (outer loop)  the total time complexity is therefore O (|V+E|)

DAST 2005 Q.In a directed graph G, show how a vertex v might have both incoming and outgoing edges, yet its tree in the DFS forest contains only v. A. DFS(u) DFS(v) DFS(w)  wvuwvu

DAST 2005 Q.q4,5 from ex7