/
CSE 421 Algorithms Richard Anderson CSE 421 Algorithms Richard Anderson

CSE 421 Algorithms Richard Anderson - PowerPoint Presentation

newson
newson . @newson
Follow
342 views
Uploaded On 2020-06-22

CSE 421 Algorithms Richard Anderson - PPT Presentation

Winter 2019 Lecture 5 Announcements Reading Chapter 3 Mostly review Start on Chapter 4 Homework 2 Available Due Wednesday January 23 No class on Monday Review from Wednesday Run time function Tn ID: 783533

vertices graph edges vertex graph vertices vertex edges search bipartite odd cycle layer edge degree time topological find layers

Share:

Link:

Embed:

Download Presentation from below link

Download The PPT/PDF document "CSE 421 Algorithms Richard Anderson" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

CSE 421Algorithms

Richard Anderson

Winter

2019

Lecture 5

Slide2

Announcements

Reading

Chapter 3 (Mostly review)

Start on Chapter 4

Homework 2 Available

Due Wednesday, January 23

No class on Monday

Slide3

Review from Wednesday

Run time function T(n)

T(n) is the maximum time to solve an instance of size n

Disregard constant functions

T(n

) is O(f(n)) [T : Z

+

 R

+

]

If n is sufficiently large, T(n) is bounded by a constant multiple of f(n)

Exist c, n

0

, such that for n > n

0

, T(n) < c f(n)

Slide4

Graph Theory

G = (V, E)

V – vertices

E – edges

Undirected graphs

Edges sets of two vertices {u, v}

Directed graphs

Edges ordered pairs (u, v)Many other flavorsEdge / vertices weightsParallel edgesSelf loops

Explain that there will be some review from 326

By default |V| = n and |E| = m

Slide5

Definitions

Path: v

1

, v

2

, …,

v

k, with (vi, vi+1) in ESimple PathCycleSimple Cycle

NeighborhoodN(v)DistanceConnectivityUndirectedDirected (strong connectivity)TreesRootedUnrooted

Slide6

Graph Representation

a

b

c

d

V = { a, b, c, d}

E = { {a, b}, {a, c}, {a, d}, {b, d} }

a

b

c

d

b

c

d

a

d

a

a

b

1

1

1

1

0

1

1

0

0

1

1

0

Incidence Matrix

Adjacency List

Slide7

Graph search

Find a path from s to t

S = {s

}

while S is not empty

u = Select(S)

visit u

foreach v in N(u) if v is unvisited

Add(S, v)

Pred

[v] = u

if (v = t) then path found

Slide8

Breadth first search

Explore vertices in layers

s in layer 1

Neighbors of s in layer 2

Neighbors of layer 2 in layer 3 . . .

s

Slide9

Key observation

All edges go between vertices on the same layer or adjacent layers

2

8

3

7

6

5

4

1

Slide10

Bipartite Graphs

A graph V is bipartite if V can be partitioned into V

1

, V

2

such that all edges go between V

1

and V2A graph is bipartite if it can be two colored

Slide11

Can this graph be two colored?

Slide12

Algorithm

Run BFS

Color odd layers red, even layers blue

If no edges between the same layer, the graph is bipartite

If edge between two vertices of the same layer, then there is an odd cycle, and the graph is not bipartite

Slide13

Theorem: A graph is bipartite if and only if it has no odd cycles

If a graph has an odd cycle it is not bipartite

If a graph does not have an odd cycle, it is bipartite

Slide14

Lemma 1

If a graph contains an odd cycle, it is not bipartite

Slide15

Lemma 2

If a BFS tree has an

intra-level edge

, then the graph has an odd length cycle

Intra-level edge: both end points are in the same level

Slide16

Lemma 3

If a graph has no odd length cycles, then it is bipartite

No odd length cycles implies no intra-level edges

No intra-level edges implies two colorability

Slide17

Graph Search

Data structure for next vertex to visit determines search order

Slide18

Graph search

Breadth First Search

S

= {s

}

while S is not empty

u =

Dequeue

(S) if u is unvisited visit u foreach

v in N(u)

Enqueue

(S, v)

Depth First Search

S = {s}

while S is not empty u = Pop(S) if u is unvisited visit u foreach v in N(u) Push(S, v)

Slide19

Breadth First Search

All edges go between vertices on the same layer or adjacent layers

2

8

3

7

6

5

4

1

Slide20

Depth First Search

Each edge goes between vertices on the same branch

No cross edges

1

2

5

6

12

7

4

3

8

9

10

11

Slide21

Connected Components

Undirected Graphs

Slide22

Computing Connected Components in O(n+m) time

A search algorithm from a vertex v can find all vertices in v’s component

While there is an unvisited vertex v, search from v to find a new component

Slide23

Directed Graphs

A Strongly Connected Component is a subset of the vertices with paths between every pair of vertices.

Slide24

Identify the Strongly Connected Components

There is an O(n+m) algorithm that we will not be covering

Slide25

Strongly connected components can be found in O(n+m) time

But it’s tricky!

Simpler problem: given a vertex v, compute the vertices in v’s scc in O(n+m) time

Slide26

Topological Sort

Given a set of tasks with precedence constraints, find a linear order of the tasks

142

143

311

341

351

333

332

312

431

421

451

Slide27

Find a topological order for the following graph

E

F

D

A

C

B

K

J

G

H

I

L

Slide28

If a graph has a cycle, there is no topological sort

Consider the first vertex on the cycle in the topological sort

It must have an incoming edge

B

A

D

E

F

C

Slide29

Lemma: If a graph is acyclic, it has a vertex with in degree 0

Proof:

Pick a vertex v

1

, if it has in-degree 0 then done

If not, let (v

2

, v1) be an edge, if v2 has in-degree 0 then doneIf not, let (v3, v2) be an edge . . .If this process continues for more than n steps, we have a repeated vertex, so we have a cycle

Slide30

Topological Sort Algorithm

While there exists a vertex v with in-degree 0

Output vertex v

Delete the vertex v and all out going edges

E

F

D

A

C

B

K

J

G

H

I

L

Slide31

Details for O(n+m) implementation

Maintain a list of vertices of in-degree 0

Each vertex keeps track of its in-degree

Update in-degrees and list when edges are removed

m edge removals at O(1) cost each