## Are you sure?

This action might not be possible to undo. Are you sure you want to continue?

)

Intuition: BFS(vertex v ) To start, all vertices are unmarked. • Start at v . Visit v and mark as visited. • Visit every unmarked neighbour ui of v and mark each ui as visited. • Mark v ﬁnished. • Recurse on each vertex marked as visited in the order they were visited.

u1 1 u2 3 u3 4 v v 2 1

**BFS of an undirected Graph
**

2 v 1 7 2 1 v

5 6

3

4

8

5 6

3

4

49

Q: What information about the graph can a BFS be used to ﬁnd? • the shortest path from v to any other vertex u and this distance d(v ) • Whether the graph is connected. we call this a spanning-t Q: What is an appropriate ADT to implement a BFS given an adjacency list representation of a graph? a FIFO (ﬁrst in. ﬁrst out) queue. which has the operations: • ENQUEUE(Q.v) • DEQUEUE(Q) • ISEMPTY(Q) Q: What information will we need to store along the way? • the current node • the predecessor • the distance so far from v 50 . Q: What does the BFS construct? a BFS tree that visits every node connected to v . Number of connected components.

BFS(G=(V. ENQUEUE(Q. for each edge (u. \\ "not connected" end for initialize an empty queue Q. end while END BFS 51 .v) for all vertices u in V color[u] := black d[u] := infinity. p[v] := NIL.w) in E do if (color[w] == black) then color[w] := green. end if end for color[u] := red.w). d[v] := 0. the distance from v ).E). ENQUEUE(Q.v). d[w] := d[u] + 1. color[v] := green.e.. p[w] := u.The BFS Algorithm We will use p[v] to represent the predecessor of v and d[v] to represent the number of edges from v (i. while not ISEMPTY(Q) do u := DEQUEUE(Q). \\ we use infinity to denote p[u] := NIL.

• If not.v) Q: How many times is each node ENQUEUEed? at most once. then we may have to call BFS on more than one start vertex in order to see the whole graph. • If the graph is connected (in the undirected case) or stronglyconnected (in the directed case). the adjacency list of each node is examined at most once. Therefore. NOTES: • BFS will visit only those vertices that are reachable from v .Complexity of BFS(G. at which point it is coloured green. when it is black. then this will be all the vertices. 52 . Q: Prove that d [u ] really does represent the length of the shortest path (in terms of number of edges) from v to u. so that the total running time of BFS is O(n + m) or linear in the size of the adjacency list.

Depth-First Search Intuition: DFS(G. – mark the traversed edge as a back-edge. 53 . – Mark as visited and the edge that is traversed as a DFS edge. back up to the previous vertex • When the current vertex has only visited neighbours left mark as ﬁnished (white). if the vertex has been visited. • Continue. • Otherwise. • Walk as far as possible away from v visiting vertices • If the current vertex has not been visited.v) All vertices and edges start out unmarked. • Backtrack to the ﬁrst vertex that is not ﬁnished.

Q: Does it ﬁnd the shortest path between v and all other vertices? no 54 .Example. v v Back!edge DFS!edge v v v v Just like BFS. DFS constructs a spanning-tree and gives connected component information.

v)) • POP(S) • ISEMPTY(S) Q: What additional data (for each vertex) should we keep in order to easily determine whether an edge is a cross-edge or a DFSedge? • d[v ] will indicate the discovery time • f [v ] will indicate the ﬁnish time. (u.Implementing a DFS Q: Which ADT would be the most helpful for implementing DFS given an adjacency list representation of G? A stack S to store edges with the operations • PUSH(S. 55 .

NIL)). f[v] := infinity. // Marks the end of v’s neighbors for each edge (v. p[s] := NIL.w) in E do PUSH(S.(s.(s. d[v] := time.E).(v. PUSH(S. time := time + 1. color[s] := green. f[u] := time.s) for all vertices v in V color[v] := black. color[u] := white. end for (*) end if end while END DFS 56 .v) := POP(S).v) in E do PUSH(S.v)). for each edge (s. d[v] := infinity. end for initialize an empty stack S. if (v == NIL) then // Done with u time := time + 1.s) DFS(G=(V.NIL)). time := 0.Algorithm DFS(G. PUSH(S.(v. d[s] := 0. p[v] := NIL.w)). p[v] := u. end for while not ISEMPTY(S) do (u. end if else if (color[v] == black) then color[v] := green.

e.when the node is green and the neighbour is black • Therefore. • So the total running time is just like for BFS.. Q: Is the DFS tree unique for a given graph G starting at s? no For certain applications.s) Q: How many times does DFS visit the neighbours of a node? • once.. or the DFS edges form a tree called the DFS-tree. we need to distinguish between different types of edges in E : 57 . linear in the size of the adjacency list. Note that the gold edges. Θ(n + m) i.. the adjacency list of each vertex is visited at most once.Complexity of DFS(G.

• Cross-Edges∗ are all the other edges that are not part of the DFS tree (from a vertex u to another vertex v that is neither an ancestor nor a descendent of u in the DFS tree). If the color of v is green ins 58 .We can specify edges on DFS-tree according to how they are traversed during the search. • Tree-Edges are the edges in the DFS tree. Q: Which variable facilitates distinguishing between these edges? p[v ] Q: How can a DFS be used to determine whether a graph G has any cycles? (Note: A cycle is a path from a vertex u to itself. • Forward-Edges∗ are edges from a vertex u to a descendent of u in the DFS tree. • Back-Edges are edges from a vertex u to an ancestor of u in the DFS tree.) It is not hard to see that there is a cycle in G if and only if there are any c Q: How can we detect cross-edges during a DFS? Add a test after the line marked by (*) in DFS.

59 . E ) be a connected. the thin ones are not.Minimum Cost Spanning Trees (MCSTs) • Let G = (V. The ﬁrst one is a tree and the other two are not. Thick edges are in A. • A tree is a subset of edges A ⊂ E such that A is connected and does not contain a cycle. Tree Not a tree (has a cycle) Not a tree (not connected) A spanning tree is a tree A such that every vertex v ∈ V is an endpoint of at least one edge in A. undirected graph with edge weights w(e) for each edge e ∈ E .

BFS A minimum cost spanning tree (MCST) is a spanning tree A such that the sum of the weights is minimized for all possible spanning trees B . Formally: w(A) = e∈A w(e) ≤ w(B ) for all other spanning trees B . 5 8 2 4 6 2 8 4 6 5 Minimum Cost Spanning Tree 3 3 3 3 3 Spanning tree. Q: What algorithms have we seen to construct a spanning tree? DFS. but not minimum cost 3 Two different MCSTs on the same graph 60 .Q: How many edges must any spanning tree contain? n − 1 edges where |V | = n How would you prove this is true? (proof by induction on n).

Prim’s Algorithm Prim’s algorithm uses a Priority Queue ADT. A priority queue is just like a queue except that every item in the queue has a priority (usually just a number).Q: What is an example of an application in which you would we want to be able to ﬁnd a MCST? A: Imagine you have a network of computers that are connected by various links. a priority queue consists of • a set of elements • each element has a priority • The operations: – ISEMPTY(): return whether the priority queue is empty – ENQUEUE(x. More formally. with priority valu – EXTRACT MIN(): remove and return an element x with the smal 61 .p): insert an element x in the set. The ﬁrst is Prim’s Algorithm. Some We will look at two algorithms for constructing MCSTs.

- EE5301-Alg
- Basic Mathematics.ppt
- Lecture 3
- MC0080-1
- 01 Solutions
- Data Structures & Algorithms Lab - Using 'C' - Solutions 1
- Data Structure(DS)2620001
- GATE - CS - Engineering Mathematics
- Artic
- Phisiology
- Minimum Spanning Trees
- 620001
- MC0068 August 2010 Q & Ans
- match78n3_665-674
- An Ant-Based Algorithm for Finding
- NCS.Duke
- Andrew Rosenberg- Lecture 10
- István Elek - {Iconip} - [2012]
- 13-06-adj-matrix.pdf
- Ai Two Marks
- A Supervised Word Sense Disambiguation Method Using Ontology and Context Knowledge
- Topic1 State Space Search
- Graph Theory and Combination 2
- MIT15_082JF10_lec01.ppt
- Networks in Transportation Theory
- 2 001 SECTION - A 1. This Question Consists Of
- AdvanceOS Final
- DAA QB 1.pdf
- Question
- Gaster

Skip carousel

- tmp7F8
- tmp2EBB.tmp
- tmp4359.tmp
- A Survey
- A NEW S-(a,d) ANTIMAGIC LABELING OF A CLASS OF CIRCULANT GRAPHS
- tmp3C86.tmp
- Discrete Mathematics MCQ'S
- Analysis and Design Algorithem
- tmp20DC.tmp
- tmp30E5.tmp
- tmpD746
- tmpC906
- A New S-(a,d) Antimagic Labelling of a Class of Generalized Petersen Graphs
- Discrete Mathematics
- tmp6B5B.tmp
- tmpDE4.tmp
- tmp18A.tmp
- tmpB0E6
- Network Bucket Testing
- Discrete Mathematics MCQ'S
- tmp3C0A.tmp
- tmpE648
- tmpBF3
- Algorithm and Data Structure
- Tmp 1387
- Context Aware Decision Support Mobile Application for Recommendation in the Domain of Hotels
- A Survey on Subgraph Matching Algorithm for Graph Database
- tmp9F1C

Skip carousel

- A Review on Image Inpainting with K-Nearest Neighbor (KNN) Method
- Optimization of Water Distribution Network for Dharampeth Area
- tmpC8D2.tmp
- Personalized Gesture Recognition with Hidden Markove Model and Dynamic Time Warping
- 68537_1985-1989
- tmp2A0A.tmp
- A System for Efficient Retrieval of the Images from Large Datasets using Ripplet Transform and Edge Histogram Detector
- Implementation of Feed Forward Neural Network for Image Compression on FPGA
- tmpDE32.tmp
- Review on Advanced Prediction of Difficult Keyword Queries Over Databases
- Analyzing Sentiment at Sentence-Level on Tweets using Hybrid Systems
- Weighted Density based Error Optimization for Classical Dataset
- tmpDBB7.tmp
- An Algorithm to Improve Accuracy of Recommendation System
- 63607_2010-2014
- Image Processing Techniques For Quality Checking In Food Industry
- Digital Image Watermarking Based on LSB for RGB Image

Sign up to vote on this title

UsefulNot usefulClose Dialog## Are you sure?

This action might not be possible to undo. Are you sure you want to continue?

Close Dialog## This title now requires a credit

Use one of your book credits to continue reading from where you left off, or restart the preview.

Loading