Presentation is loading. Please wait.

Presentation is loading. Please wait.

COSC 3101NJ. Elder Assignment 2 Remarking Assignment 2 Marks y = 0.995x + 8.47 R 2 = 0.8558 0 10 20 30 40 50 60 70 80 90 100 050100 Old Mark New Mark.

Similar presentations


Presentation on theme: "COSC 3101NJ. Elder Assignment 2 Remarking Assignment 2 Marks y = 0.995x + 8.47 R 2 = 0.8558 0 10 20 30 40 50 60 70 80 90 100 050100 Old Mark New Mark."— Presentation transcript:

1 COSC 3101NJ. Elder Assignment 2 Remarking Assignment 2 Marks y = 0.995x + 8.47 R 2 = 0.8558 0 10 20 30 40 50 60 70 80 90 100 050100 Old Mark New Mark

2 COSC 3101NJ. Elder Section V. Graph Algorithms

3 COSC 3101NJ. Elder Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouring vertices: Undiscovered vertices are coloured white Just discovered vertices (on the wavefront) are coloured grey. Previously discovered vertices (behind wavefront) are coloured black.

4 COSC 3101NJ. Elder Breadth-First Search Algorithm Each vertex assigned finite d value at most once. d values assigned are monotonically increasing over time. Q contains vertices with d values {i, …, i, i+1, …, i+1} Time = O(V+E)

5 COSC 3101NJ. Elder Depth-First Search Explore every edge, starting from different vertices if necessary. As soon as vertex discovered, explore from it. Keep track of progress by colouring vertices: White: undiscovered vertices Grey: discovered, but not finished (still exploring from it) Black: finished (found everything reachable from it).

6 COSC 3101NJ. Elder Depth-First Search Algorithm

7 COSC 3101NJ. Elder Why is BFS O(S+V), while DFS is  (S+V)? Remember: BFS(G,s) finds the shortest paths from a specified vertex s. If G is either undirected and not connected, or directed and not strongly connected, not all vertices of the graph will be visited in BFS. In contrast, DFS(G) visits all vertices, restarting at multiple source vertices if necessary.

8 COSC 3101NJ. Elder An Application of DFS: Topological Sorting

9 COSC 3101NJ. Elder Strongly Connected Components

10 COSC 3101NJ. Elder Component Graph

11 COSC 3101NJ. Elder Lemma

12 COSC 3101NJ. Elder SCC Algorithm

13 COSC 3101NJ. Elder Lemma

14 COSC 3101NJ. Elder So why does the SCC Algorithm work?

15 COSC 3101NJ. Elder Minimum Spanning Trees Example Problem You are planning a new terrestrial telecommunications network to connect a number of remote mountain villages in a developing country. The cost of building a link between pairs of neighbouring villages (u,v) has been estimated: w(u,v). You seek the minimum cost design that ensures each village is connected to the network. The solution is called a minimum spanning tree.

16 COSC 3101NJ. Elder Building the Minimum Spanning Tree Iteratively construct the set of edges A in the MST. Initialize A to {} As we add edges to A, maintain a Loop Invariant: A is a subset of some MST Maintain loop invariant and make progress by only adding safe edges. An edge (u,v) is called safe for A iff A  {u,v}) is also a subset of some MST.

17 COSC 3101NJ. Elder Finding a safe edge Idea: Every 2 disjoint subsets of vertices must be connected by at least one edge. Which one should we choose?

18 COSC 3101NJ. Elder Some definitions A cut (S,V-S) is a partition of vertices into disjoint sets V and S-V. Edge (u,v)  E crosses cut (S, V-S) if one endpoint is in S and the other is in V-S. A cut respects A iff no edge in A cross the cut. An edge is a light edge crossing a cut iff its weight is minimum over all edges crossing the cut.

19 COSC 3101NJ. Elder Theorem Let A be a subset of some MST (S,V-S) be a cut that respects A (u,v) be a light edge crossing (S,V-S) Then (u,v) is safe for A.

20 COSC 3101NJ. Elder Proof Let T be an MST that includes A. If T contains (u,v) then we’re done. Suppose T does not contain (u,v) Can construct different MST T' that includes A  {u,v}) The edge (u,v) forms a cycle with the edges on the path from u to v in T. There is at least one edge in T that crosses the cut: let that edge be (x,y) (x,y) is not in A, since the cut (S,V-S) respects A. Form new spanning tree T' by deleting (x,y) from T and adding (u,v). w(T')  w(T), since w(u,v)  w(x,y)  T' is an MST. A  T', since A  T and (x,y)  A  A  {u,v})  T' Thus (u,v) is safe for A. All edges shown (except (u,v)) are in T

21 COSC 3101NJ. Elder Kruskal’s Algorithm for computing MST Starts with each vertex being its own component. Repeatedly merges two components into one by choosing the light edge that crosses the cut between them. Scans the set of edges in monotonically increasing order by weight. Uses a disjoint-set data structure to determine whether an edge connects vertices in different components.

22 COSC 3101NJ. Elder Kruskal’s Algorithm for computing MST Running Time = O(ElogE) = O(ElogV)

23 COSC 3101NJ. Elder Prim’s Algorithm Build one tree Start from arbitrary root r At each step, add light edge crossing cut (V A, V- V A ), where V A = vertices that A is incident on.

24 COSC 3101NJ. Elder Finding light edges quickly All vertices not in the partial MST formed by A reside in a min- priority queue. Each object is a vertex v in V-V A Key of v is minimum weight of any edge (u,v), u  V A. Each vertex knows its parent in partial MST by  [v].

25 COSC 3101NJ. Elder Prim’s Algorithm Running Time = O(Elog(V))

26 COSC 3101NJ. Elder Example: Prim's Algorithm

27 COSC 3101NJ. Elder Single-Source Shortest Paths

28 COSC 3101NJ. Elder The Problem What is the shortest driving route from Toronto to Ottawa? (e.g. MAPQuest, Microsoft Streets and Trips) Input: Directed graph G=(V,E) Weight function w: E  R

29 COSC 3101NJ. Elder Example

30 COSC 3101NJ. Elder Shortest path variants Single-source shortest-paths problem: – the shortest path from s to each vertex v. (e.g. BFS) Single-destination shortest-paths problem: Find a shortest path to a given destination vertex t from each vertex v. Single-pair shortest-path problem: Find a shortest path from u to v for given vertices u and v. All-pairs shortest-paths problem: Find a shortest path from u to v for every pair of vertices u and v.

31 COSC 3101NJ. Elder Negative-weight edges OK, as long as no negative-weight cycles are reachable from the source. If we have a negative-weight cycle, we can just keep going around it, and get w(s, v) = −∞ for all v on the cycle. But OK if the negative-weight cycle is not reachable from the source. Some algorithms work only if there are no negative-weight edges in the graph.

32 COSC 3101NJ. Elder Optimal substructure Lemma: Any subpath of a shortest path is a shortest path Proof: Cut and paste.

33 COSC 3101NJ. Elder Cycles Shortest paths can’t contain cycles: Already ruled out negative-weight cycles. Positive-weight: we can get a shorter path by omitting the cycle. Zero-weight: no reason to use them  assume that our solutions won’t use them.

34 COSC 3101NJ. Elder Output of a single-source shortest-path algorithm For each vertex v in V: d[v] = δ(s, v). Initially, d[v]=∞. Reduces as algorithms progress. But always maintain d[v] ≥ δ(s, v). Call d[v] a shortest-path estimate. π[v] = predecessor of v on a shortest path from s. If no predecessor, π[v] = NIL. π induces a tree — shortest-path tree.

35 COSC 3101NJ. Elder Initialization All shortest-paths algorithms start with the same initialization: INIT-SINGLE-SOURCE(V, s) for each v in V do d[v] ← ∞ π[v] ← NIL d[s] ← 0

36 COSC 3101NJ. Elder Relaxing an edge Can we improve shortest-path estimate for v by going through u and taking (u,v)? RELAX(u, v,w) if d[v] > d[u] + w(u, v) then d[v] ← d[u] + w(u, v) π[v] ← u

37 COSC 3101NJ. Elder General single-source shortest-path strategy 1. Start by calling INIT-SINGLE-SOURCE 2. Relax Edges Algorithms differ in the order in which edges are taken and how many times each edge is relaxed.

38 COSC 3101NJ. Elder Example: Single-source shortest paths in a directed acyclic graph (DAG) Since graph is a DAG, we are guaranteed no negative-weight cycles.

39 COSC 3101NJ. Elder Algorithm

40 COSC 3101NJ. Elder Example

41 COSC 3101NJ. Elder Example

42 COSC 3101NJ. Elder Example

43 COSC 3101NJ. Elder Example

44 COSC 3101NJ. Elder Example

45 COSC 3101NJ. Elder Example

46 COSC 3101NJ. Elder Correctness: Path relaxation property (Lemma 24.15)

47 COSC 3101NJ. Elder Correctness of DAG Shortest Path Algorithm Because we process vertices in topologically sorted order, edges of any path are relaxed in order of appearance in the path.  Edges on any shortest path are relaxed in order.  By path-relaxation property, correct.

48 COSC 3101NJ. Elder Example: Dijkstra’s algorithm Applies to general weighted directed graph (may contain cycles). But weights must be non-negative. Essentially a weighted version of BFS. Instead of a FIFO queue, uses a priority queue. Keys are shortest-path weights (d[v]). Maintain 2 sets of vertices: S = vertices whose final shortest-path weights are determined. Q = priority queue = V-S.

49 COSC 3101NJ. Elder Dijkstra’s algorithm Observations: Looks a lot like Prim’s algorithm, but computing d[v], and using shortest-path weights as keys. Dijkstra’s algorithm can be viewed as greedy, since it always chooses the “lightest” (“closest”?) vertex in V − S to add to S.

50 COSC 3101NJ. Elder Dijkstra’s algorithm: Analysis Analysis: Using binary heap, each operation takes O(logV) time  O(ElogV)

51 COSC 3101NJ. Elder Example

52 COSC 3101NJ. Elder Example

53 COSC 3101NJ. Elder Example

54 COSC 3101NJ. Elder Example

55 COSC 3101NJ. Elder Example

56 COSC 3101NJ. Elder Example

57 COSC 3101NJ. Elder Correctness of Dijkstra’s algorithm Loop invariant: At the start of each iteration of the while loop, d[v] = δ(s, v) for all v in S. Initialization: Initially, S is empty, so trivially true. Termination: At end, Q is empty  S = V  d[v] = δ(s, v) for all v in V. Maintenance: Need to show that d[u] = δ(s, u) when u is added to S in each iteration. Proof is by contradiction (see text).


Download ppt "COSC 3101NJ. Elder Assignment 2 Remarking Assignment 2 Marks y = 0.995x + 8.47 R 2 = 0.8558 0 10 20 30 40 50 60 70 80 90 100 050100 Old Mark New Mark."

Similar presentations


Ads by Google