CSE 502N Fundamentals of Computer Science - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

CSE 502N Fundamentals of Computer Science

Description:

Basis for Prim's minimum-spanning tree and ... Given a graph G = (V,E) and a source vertex s, breadth-first search computes the ... Optimal substructure ... – PowerPoint PPT presentation

Number of Views:36
Avg rating:3.0/5.0
Slides: 11
Provided by: davidedwa
Category:

less

Transcript and Presenter's Notes

Title: CSE 502N Fundamentals of Computer Science


1
(No Transcript)
2
CSE 502NFundamentals of Computer Science
  • Fall 2004
  • Lecture 16
  • Breadth-first Search (CLRS 22.2)
  • Dijkstras Shortest Path (CLRS 24.3)

3
Breadth-first search
  • Basis for Prims minimum-spanning tree and
    Dijkstras single-source shortest paths algorithm
  • Given a graph G (V,E) and a source vertex s,
    breadth-first search computes the smallest number
    of edges from s to each reachable vertex
  • G may be directed or un-directed
  • Produces a breadth-first tree with root s that
    contains all reachable vertices
  • Path from s to reachable vertex v in tree
    corresponds to shortest path from s to v in G
  • BFS constructs the breadth-first tree by
    discovering all reachable vertices at the current
    distance k before moving to vertices at distance
    k 1

4
Breadth-first search
  • Assign colors to vertices to denote status
  • White undiscovered vertex
  • Gray discovered vertex with unexplored adjacency
    list
  • Black discovered vertex, explored adjacency list
  • All vertices adjacent to a black vertex are
    discovered
  • Gray vertices may have adjacent white vertices
  • Frontier between discovered and undiscovered
    vertices
  • When a white vertex v is discovered while
    scanning the adjacency list of previously
    discovered vertex u, vertex v and edge (u,v) are
    added to the breadth-first tree
  • Vertex u is the predecessor or parent of v in the
    treepv u

5
BFS Pseudocode
6
BFS Analysis
  • Loop invariant the queue consists of the set of
    gray vertices
  • Each node is enqueued and dequeued at most once
  • Queuing operations are O(1), thus O(V) time is
    spent on queuing operations
  • Each adjacency list is scanned at most once
  • Sum of lengths of adjacency lists is Q(E), time
    spent scanning is O(E)
  • Initialization overhead is O(V)
  • Total running time is O(VE)

7
Single-source shortest paths
  • Given a weighted, directed graph G (V,E), find
    the shortest path from a given source vertex s Î
    V to each vertex v Î V
  • Also can be used to solve the single-destination
    and single-pair shortest-path problems
  • Let w(u,v) be the real valued weight of edge
    (u,v)
  • Let w(p) be the weight of path p áv1,v2,,vkñ
  • Equal to the sum of the weights of its
    constituent edges
  • Let d(u,v) be the shortest-path weight from u to
    v
  • A shortest path from u to v is any path p with
    weightw(p) d(u,v)

8
Optimal substructure
  • Most shortest-path algorithms rely on the
    property that a shortest path between two
    vertices contains other shortest paths within it
  • Given a weighted, directed graph G (V,E) with
    real valued edge weights
  • Let p áv1,v2,,vkñ be a shortest path from
    vertex v1 to vertex vk
  • For any i and j such that 1 i j k, let pij
    ávi,vi1,,vjñ be the subpath of p from vertex
    vi to vertex vj
  • Then, pij is a shortest path from vi to vj

9
Relaxation
  • For each vertex v Î V, we maintain a
    shortest-path estimate dv which is an upper
    bound on the weight of a shortest path from s to
    v
  • Initialize shortest-path estimates and
    predecessors in time Q(V)
  • Relaxing an edge (u,v) consists of testing
    whether the current shortest path to v can be
    improved by going through u
  • If so, dv and pv are updated
  • Shortest-path algorithms call Initialize-Single-So
    urce then repeatedly relax edges
  • Relaxation is the only means by which
    shortest-path estimates and predecessors change

10
Dijkstras algorithm
  • Solves the single-source shortest-paths problem
    on weighted, directed graph with nonnegative edge
    weights
  • Maintain a set S of vertices whose final
    shortest-path weights from the source s have been
    determined
  • Algorithm repeats the following steps until all
    reachable vertices have been added to S
  • Select the vertexu Î V S with
    minimumshortest-path estimate
  • Add u to S
  • Relax all edges leaving u
  • Uses a min-priority queue Q keyed by
    shortest-path estimates
Write a Comment
User Comments (0)
About PowerShow.com