09 - Queues, Stacks, Graph Traversing Data Structures and...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
Queues, Stacks, Graph Traversing Data Structures and Algorithms Andrei Bulatov
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Algorithms – Queues, Stacks, Graph Traversing 9-2 Graph Reminder Vertices and edges Nodes and arcs Representation of graphs: -- adjacency matrix -- adjacency lists Degrees of vertices, indegree, outdegree; regular graphs Walks, paths, and cycles; lengths Connectivity, connected components Trees, root, leaves, parent and child, descendant and ancestor
Background image of page 2
Algorithms – Queues, Stacks, Graph Traversing 9-3 Graph Traversing Often we need to visit every vertex of a graph There are many ways to do that, two are most usual: breadth first search and depth first search In both cases we start with some vertex s For BFS: - visit neighbors of s - then visit neighbors of neighbors in turn - this data structure is called a queue s t u w v x t u v w x
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Algorithms – Queues, Stacks, Graph Traversing 9-4 Graph Traversing: BFS Breadth First Search(G,s) set Discov[s]:=true and Discov[v]:=false for v s Enqueue(Q,s) while Q is not empty do set u:=Dequeue(Q) for each (u,v) E do if Discov[v]=false then do set Discov[v]:=true; Enqueue(Q,v) endif endfor endwhile
Background image of page 4
Algorithms – Queues, Stacks, Graph Traversing 9-5 Example s t u w v x
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Algorithms – Queues, Stacks, Graph Traversing 9-6 Queues Through Array If we store a queue in an array, we need two pointers to the head and to the tail of the queue 7 2 4 6 8 3 9 6 8 5 head tail tail head
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/19/2009 for the course CS CMPT 307 taught by Professor A.bulatov during the Fall '09 term at Simon Fraser.

Page1 / 26

09 - Queues, Stacks, Graph Traversing Data Structures and...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online