33
15.082J, 6.855J, and ESD.78J Sept 16, 2010 Lecture 3. Graph Search Breadth First Search Depth First Search Intro to program verification Topological Sort

15.082J, 6.855J, and ESD.78J Sept 16, 2010

  • Upload
    naava

  • View
    33

  • Download
    0

Embed Size (px)

DESCRIPTION

15.082J, 6.855J, and ESD.78J Sept 16, 2010. Lecture 3. Graph Search Breadth First Search Depth First Search Intro to program verification Topological Sort. Overview. Today: Different ways of searching a graph a generic approach breadth first search depth first search - PowerPoint PPT Presentation

Citation preview

Page 1: 15.082J, 6.855J, and ESD.78J Sept 16, 2010

15.082J, 6.855J, and ESD.78JSept 16, 2010

Lecture 3. Graph SearchBreadth First Search

Depth First Search

Intro to program verification

Topological Sort

Page 2: 15.082J, 6.855J, and ESD.78J Sept 16, 2010

2

Overview

Today: Different ways of searching a grapha generic approachbreadth first searchdepth first searchprogram verificationdata structures to support network search topological order

Fundamental for most algorithms considered in this subject

Page 3: 15.082J, 6.855J, and ESD.78J Sept 16, 2010

3

Searching a Directed GraphALGORITHM SEARCH

INPUT: A directed network G, and node s

OUTPUT: The set S = {j : there is a directed path from s to j in G}. These are the nodes reachable from s. For each node j S\s, ∈pred(j) is a node that precedes j on some path from s; e.g. (pred(2) = 1, pred(8) = 4

1

2 4

5

3 6 9

7

8

1

2 4

5

3 6 9

7

8

Page 4: 15.082J, 6.855J, and ESD.78J Sept 16, 2010

Marked nodes, admissible arcs

A node is either marked or unmarked. Initially only node s is marked. If a node is marked, it is reachable from node s.

An arc (i,j) ∈ A is admissible if node i is marked and j is not.

4

1

2 4

5

3 6 9

7

8

1

2 4

5

3 6 9

7

8 Marked nodes

Admissible arcs

Page 5: 15.082J, 6.855J, and ESD.78J Sept 16, 2010

5

Scanning arcs

1 2 53

1

2

3

5

CurrentArc(1) CurrentArc(1) CurrentArc(1)

Scan through the arc list for the selected node, and keep track using current arc. Stop when an admissible arc is identified or when the arc list is fully scanned

Page 6: 15.082J, 6.855J, and ESD.78J Sept 16, 2010

6

Algorithm SearchInitialize as follows:

unmark all nodes in N;mark node s;pred(s) = 0; {that is, it has no predecessor}LIST = {s}

while LIST ≠ ø doselect a node i in LIST;if node i is incident to an admissible arc (i,j) then

mark node j;pred(j) := i;add node j to the end of LIST;

else delete node i from LIST

The algorithm in the book also keeps track of the order in which nodes are marked.

Page 7: 15.082J, 6.855J, and ESD.78J Sept 16, 2010

Breadth first search

7

It is a breadth first search (bfs) if the selected node is the first node on LIST.

Breadth First Search Animation

Page 8: 15.082J, 6.855J, and ESD.78J Sept 16, 2010

8

More on Breadth First Search

Theorem. The breadth first search tree is the “shortest path tree”, that is, the path from s to j in the tree has the fewest possible number of arcs.

1 2

3

2

1

2 4

5

3 6 9

7

8

110

21

5

3

4

6

8

6

7

96 99

3

3

The numbers next to the nodes are the distances from node 1.

Page 9: 15.082J, 6.855J, and ESD.78J Sept 16, 2010

Depth first search

9

It is a depth first search (dfs) if the selected node is the last node on LIST.

Depth First Search Animation

Page 10: 15.082J, 6.855J, and ESD.78J Sept 16, 2010

10

1

3

2 9

8

7

54 8

6

7

9

6

5

4

2 3

1

The depth first search tree

Note that each induced subtree has consecutively labeled nodes.

(The descendents are visited in order.)

Page 11: 15.082J, 6.855J, and ESD.78J Sept 16, 2010

Algorithm Analysis

How does one prove that an algorithm is correct?

How does one prove that it terminates in finite time?

How does one obtain a tight upper bound on running time?

11

Page 12: 15.082J, 6.855J, and ESD.78J Sept 16, 2010

Some useful approaches

Subdivide the algorithm into “chunks”.

Invariants: properties that are true throughout the running of the algorithm

Things that change: functions that increase monotonically every time the algorithm reenters the same loop.

12

Page 13: 15.082J, 6.855J, and ESD.78J Sept 16, 2010

13

Algorithm SearchInitialize while LIST ≠ ø do

select a node i in LIST;if node i is incident to an admissible arc (i,j) then

mark node j;pred(j) := i;add node j to the end of LIST;

else delete node i from LIST

loop

Page 14: 15.082J, 6.855J, and ESD.78J Sept 16, 2010

14

Algorithm Invariants

Select Node 2

LIST 1 2 4 85 6 9 7

8

7

5

3

1

2 4

5

3 6 9

7

8

11 1

22

1

22 4 8 44 884

5

4

5

66

5

6 96 9

7

9

77

9966

55

442

Invariants: whenever control of the program is at loop:1.Any marked node is reachable from s.2.All nodes on LIST are marked.3.If a marked node j is not on LIST, then A(j) has been fully scanned.

Page 15: 15.082J, 6.855J, and ESD.78J Sept 16, 2010

Proving the correctness of the invariants

An Important step in proving algorithm correctness: Prove that the algorithm invariants are true using induction.

• Prove that they are true after the initialization

• Assuming that they are true at the beginning of the k-th iteration of the while loop, prove that they are true at the beginning of the subsequent iteration of the while loop.

15

Page 16: 15.082J, 6.855J, and ESD.78J Sept 16, 2010

Things that change and proof of finiteness

Things that change between successive times that the control of the program is at loop:

1. Either a new node is marked and added to LIST, or a new

node is fully scanned and deleted from LIST.

16

Number of iterations of while loop is at most 2n.

Therefore the algorithm terminates in O(n) calls of the “while loop.”

Page 17: 15.082J, 6.855J, and ESD.78J Sept 16, 2010

Proof of Correctness

The algorithm terminates when LIST = .∅

17

Let S = marked nodes.

Let T = unmarked nodes.

1

2

4

5 t

6

s

3

s

5

4

2

6S

T

Then no arc (i,j) is directed from S to T. Otherwise, by Invariant 2, j would have been marked when (i, j) was scanned.

By invariant 1, all nodes in S are reachable from s.By invariant 3, all arcs out of S have been scanned.

Therefore, no node in T is reachable from s.

Page 18: 15.082J, 6.855J, and ESD.78J Sept 16, 2010

18

Cutset Theorem

Corollary of algorithm’s correctness. There is no directed path from s to t if and only if the following is true: there is a partition of the node set N into subsets S and T = N - S such that there is no arc directed from a node in S to a node in T.

S

sT

t

Page 19: 15.082J, 6.855J, and ESD.78J Sept 16, 2010

Running time analysis

19

Initialize. while LIST ≠ ø do

select a node i in LIST;if node i is incident to an admissible arc (i,j) then

mark node j;pred(j) := i;add node j to the end of LIST;

else delete node i from LIST

loop

Total time spent in while loop (other than arc scans)• O(1) time per loop• < 2n iterations of the loop• O(n) time in total

Total time spent in scanning arcs• O(1) time per arc scanned• m arcs• O(m) time in total.

Running time: O(n + m)

Page 20: 15.082J, 6.855J, and ESD.78J Sept 16, 2010

20

Initialize

Initializebegin

unmark all nodes in N;mark node s;pred(s) = 0; {that is, it has no predecessor}LIST = {s}

end

Unmarking takes O(n)All else takes O(1)

Page 21: 15.082J, 6.855J, and ESD.78J Sept 16, 2010

21

Theorem. Algorithm search determines all nodes reachable from node s in O(n + m) time.

Page 22: 15.082J, 6.855J, and ESD.78J Sept 16, 2010

All U.S. Presidents have worn glassesTrue

No President (before Obama) has been an only childTrue

No President was a bachelorFalse. James Buchanan was a bachelor.

George Washington grew marijuana on his Plantation. True

Mental Break

Page 23: 15.082J, 6.855J, and ESD.78J Sept 16, 2010

George Washington’s false teeth were made of wood.False. They were made of whale bone.

Three of the first 10 Presidents died on July 4.True. Thomas Jefferson, John Adams, James Monroe

John Quincy Adams kept a pet alligator in the East Room of the White House

True.

Calvin Coolidge believed that the world was flat. False. But Andrew Jackson did.

Mental Break

Page 24: 15.082J, 6.855J, and ESD.78J Sept 16, 2010

24

Finding all connected components in an undirected graph

Breadth first search will find a connected component of

an undirected graph in time proportional to the number

of arcs in the component. (A component of an

undirected graph is a maximally connected subgraph.)

To find all components: Maintain a set U of unmarked

nodes.

• Delete a node from U after it is marked.

• After each component is searched, select a node of U

and begin a search.

• Running time O(1) per selection and deletion

Comp 1

Comp 2

Comp 3

Page 25: 15.082J, 6.855J, and ESD.78J Sept 16, 2010

25

Proof of bfs Theorem

Theorem. The breadth first search tree is the “shortest path tree”,

that is, the path from s to j in the tree has the fewest possible

number of arcs.

Let d(j) be the fewest number of arcs on a path from s to j.

Suffices to prove a 4th invariant: 4. If d(i) < d(j), then i is marked before j.

If the 4th invariant is true, then nodes are marked in order of increasing distance from node s.

Page 26: 15.082J, 6.855J, and ESD.78J Sept 16, 2010

26

Select node 5

LIST 5 3 4

52

4

1

2 4

5

3 6 9

7

8

311

2

5

3

2 4

5

4. If d(i) < d(j), then i is marked before j. (Note that no unmarked node has a distance that is less than d(4).)

Page 27: 15.082J, 6.855J, and ESD.78J Sept 16, 2010

27

Preliminary to Topological Sorting

COROLLARY 2. If G has no directed cycle, then one can relabel the nodes so that for each arc (i,j), i < j.

LEMMA. If each node has at least one arc going out, then the first inadmissible arc of a depth first search determines a directed cycle.

COROLLARY 1. If G has no directed cycle, then there is a node in G with no arcs going out. Similarly, there is at least one node in G with no arcs coming in.

1

2

4

5

6

7

8

7

5

6 1

2

Page 28: 15.082J, 6.855J, and ESD.78J Sept 16, 2010

28

Topological ordering

INITIALIZE as follows:for all i ∈ N do indegree(i) := 0;for all (i,j) ∈ A do indegree(j) := indegree(j) + 1;LIST := ;∅next := 0; for all i ∈ N do if indegree(i) = 0, then LIST := LIST ∪ { i };while LIST ≠ ∅ do

select a node i from LIST and delete it from LIST;next := next + 1;order(i) := next;for all (i,j) ∈ A(i) do indegree(j) := indegree(j) – 1; if indegree(j) = 0 then LIST := LIST ∪ { j };

if next < n then the network contains a directed cycleelse the network is acyclic and the order is topological

Animation

Page 29: 15.082J, 6.855J, and ESD.78J Sept 16, 2010

29

Invariant For Topological Sorting

A node is called marked when it receives an order.

INVARIANT (at the beginning of the while loop)

1. indegree(i) is the number of arcs directed to i from nodes that are not marked.

Thus if j is on LIST, then there are no arcs into node j from unmarked nodes.

If the algorithm ends before labeling all nodes, then there is a directed cycle in the unmarked nodes. Every unmarked node has at least one incoming arc, and so

there is a directed cycle.

Page 30: 15.082J, 6.855J, and ESD.78J Sept 16, 2010

30

More on Topological Sorting

Runs in O(n+m) time.

Useful starting point for many algorithms that involve acyclic graphs.

Page 31: 15.082J, 6.855J, and ESD.78J Sept 16, 2010

31

Summary on Graph Search

Graph Search Finds all nodes reachable from s in O(m) time. Determine the connected components of an

undirected graph. Breadth first search Depth first search

Algorithm Validation (proofs of correctness).• Prove invariants using induction• Establish things that change

Page 32: 15.082J, 6.855J, and ESD.78J Sept 16, 2010

32

Summary on Graph Search

Topological sort (or order);

Running time is O(n+m) using simple data structures and algorithms.

Very important for preprocessing.

Page 33: 15.082J, 6.855J, and ESD.78J Sept 16, 2010

MITOpenCourseWarehttp://ocw.mit.edu

15.082J / 6.855J / ESD.78J Network OptimizationFall 2010

For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.