alyce brady cs 510: computer algorithms breadth-first graph traversal algorithm
Post on 20-Dec-2015
227 Views
Preview:
TRANSCRIPT
Alyce BradyAlyce Brady
CS 510: Computer Algorithms CS 510: Computer Algorithms
Breadth-FirstGraph
Traversal Algorithm
Search vs Traversal
Search: Look for a given nodeSearch: Look for a given node– stop when node found, even if not all stop when node found, even if not all
nodes were visitednodes were visited Traversal: Always visit all nodesTraversal: Always visit all nodes
Breadth-first Search
Similar to Breadth-first Traversal of a Similar to Breadth-first Traversal of a Binary TreeBinary Tree
Choose a starting vertexChoose a starting vertex Search all adjacent verticesSearch all adjacent vertices Return to each adjacent vertex in turn Return to each adjacent vertex in turn
and visit all of its adjacent verticesand visit all of its adjacent vertices
Pseudo-Code forBreadth-First Search
breadth-first-searchbreadth-first-searchmark starting vertex as visited; put on queuemark starting vertex as visited; put on queuewhile the queue is not emptywhile the queue is not empty
dequeue the next nodedequeue the next node
for all unvisited vertices adjacent to this onefor all unvisited vertices adjacent to this one–mark vertex as visitedmark vertex as visited–add vertex to queueadd vertex to queue
Was this a true search?Was this a true search?– How would we make it a true search?How would we make it a true search?
Was this a true traversal?Was this a true traversal?– How would we make it a true traversal?How would we make it a true traversal?
Time and Space Complexityfor Breadth-First Search
Time ComplexityTime Complexity– Adjacency ListsAdjacency Lists
Each node is added to queue onceEach node is added to queue once Each node is checked for each incoming edgeEach node is checked for each incoming edge O (v + e)O (v + e)
– Adjacency MatrixAdjacency Matrix Have to check all entries in matrix: O(nHave to check all entries in matrix: O(n22))
top related