csc 213 large scale programming n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Lecture 34: Relaxation-Based Approach PowerPoint Presentation
Download Presentation
Lecture 34: Relaxation-Based Approach

Loading in 2 Seconds...

play fullscreen
1 / 42

Lecture 34: Relaxation-Based Approach - PowerPoint PPT Presentation


  • 91 Views
  • Uploaded on

CSC 213 – Large Scale Programming. Lecture 34: Relaxation-Based Approach. Today’s Goals. Discuss what is meant by weighted graphs Where weights placed within Graph How to use Graph ’s weights to model problems How to solve problems once Graph is set up

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Lecture 34: Relaxation-Based Approach' - addison


Download Now An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
today s goals
Today’s Goals
  • Discuss what is meant by weighted graphs
    • Where weights placed within Graph
    • How to use Graph’s weights to model problems
    • How to solve problems once Graph is set up
  • Learn about myth & legend of EdsgarDijkstra
    • Who was he? Why should we care? How is it related?
    • What was his largest contribution to graph theory?
    • How does Dijkstra’s algorithm find smallest path?
weighted graphs
Weighted Graphs
  • Edge’s weight is cost of using edge
    • Distance, cost, travel time, &c. usable as the weight
  • Weights below are distance in miles

PVD

849

ORD

1843

142

SFO

802

LGA

1743

337

1387

HNL

2555

1099

LAX

1233

DFW

1120

MIA

cheapest path problem
Cheapest Path Problem
  • Find path with min. weight between 2 vertices
    • Sum of edge weights is the path weight
  • Consider the cheapest path from PVD to HNL
    • None of edges is cheapest in this example

PVD

849

ORD

1843

142

SFO

802

LGA

1743

337

1387

HNL

2555

1099

LAX

1233

DFW

1120

MIA

cheapest path problem1
Cheapest Path Problem
  • Subpath on shortest path is shortestpath also
    • Otherwise we would use shorter subpath
  • Tree made by all shortest paths from vertex
    • Consider all shortest paths from PVD

PVD

849

ORD

1843

142

SFO

802

LGA

1743

337

1387

HNL

2555

1099

LAX

1233

DFW

1120

MIA

dijkstra s algorithm
Dijkstra’s Algorithm
  • Finds cheapest paths from single vertex
    • Normally, computes cheapest path to all vertices
    • Stop once vertex computedfor single target vertex
  • Makes several fundamental assumptions
    • Connected graph needed when targeting all vertices
    • Only works if edge weights must be nonnegative
dijkstra s algorithm1
Dijkstra’s Algorithm
  • Grows cloud of vertices as it goes
    • Cloud starts with source vetex
    • Add vertex to cloud with each step
  • Tracks distances to each vertexnot in cloud
    • For each vertex, considers only cheapest path
    • Only uses 1 edge from cloud to vertex not in cloud
  • Each step uses vertex with smallest distance
    • Adds this vertex to cloud, if not done yet
    • Checks if creates smaller path to any vertices
edge relaxation
Edge Relaxation
  • Consider e from u to z
  • When u added to cloud
    • Check adjacent vertices
    • Assume z not in cloud
    • Found faster path!
  • Update via relaxation
    • New minimum selected:

d(u) = 50

10

u

e

z

d(z) = 75

s

edge relaxation1
Edge Relaxation
  • Consider e from u to z
  • When u added to cloud
    • Check adjacent vertices
    • Assume z not in cloud
    • Found faster path!
  • Update via relaxation
    • New minimum selected:

d(u) = 50

d(z) =60

10

u

e

z

d(z) = 75

s

edge relaxation2
Edge Relaxation
  • Consider e from u to z
  • When u added to cloud
    • Check adjacent vertices
    • Assume z not in cloud
    • Found faster path!
  • Update via relaxation
    • New minimum selected:

d(u) = 50

d(z) =60

10

u

e

z

d(z) = 75

s

edge relaxation3
Edge Relaxation
  • Consider e from u to z
  • When u added to cloud
    • Check adjacent vertices
    • Assume z not in cloud
    • Found faster path!
  • Update via relaxation
    • New minimum selected:

d(u) = 50

d(z) =60

10

u

e

z

s

dijkstra example
Dijkstra Example

0

A

4

8

2

8

2

4

1

7

B

C

D

9

3

2

5

E

F

dijkstra example1
Dijkstra Example

0

A

4

8

2

8

2

3

1

7

B

C

D

9

3

2

5

11

5

E

F

dijkstra example2
Dijkstra Example

0

A

4

8

2

8

2

3

1

7

B

C

D

9

3

2

5

8

5

E

F

dijkstra example3
Dijkstra Example

0

A

4

8

2

7

2

3

1

7

B

C

D

9

3

2

5

8

5

E

F

dijkstra example4
Dijkstra Example

0

A

4

8

2

7

2

3

1

7

B

C

D

9

3

2

5

8

5

E

F

dijkstra example5
Dijkstra Example

0

A

4

8

2

7

2

3

1

7

B

C

D

9

3

2

5

8

5

E

F

why dijkstra s algorithm works
Why Dijkstra’s Algorithm Works
  • Ultimately, Dijkstrawas smart
    • Smarter than me, if that is possible
why dijkstra s algorithm works1
Why Dijkstra’s Algorithm Works
  • Ultimately, Dijkstrawas smart
    • Smarter than me, if that is possible
why dijkstra s algorithm works2
Why Dijkstra’s Algorithm Works
  • Ultimately, Dijkstrawas smart
    • Smarter than me, if that is possible
  • Example of a greedy algorithm
    • Takes best choice at each point in time
    • Vertices added in increasing distance
    • Brings vertices closer at each step
    • Stops when vertex cannot move closer
why no negative weight edges
Why No Negative-Weight Edges?

0

A

4

8

  • Assume edge has negative weight
    • Greedily chose vertex before finding edge
    • Cloud will include only one endpoint
    • Negative weight changes everything, however
    • Vertices not added in order
  • Negative weight cycles?
    • Repeat cycle to optimize

6

5

7

1

4

7

B

C

D

0

-8

5

2

5

9

E

F

why no negative weight edges1
Why No Negative-Weight Edges?

0

A

4

8

  • Assume edge has negative weight
    • Greedily chose vertex before finding edge
    • Cloud will include only one endpoint
    • Negative weight changes everything, however
    • Vertices not added in order
  • Negative weight cycles?
    • Repeat cycle to optimize

6

5

7

1

4

7

B

C

D

0

-8

5

2

5

9

E

F

C added when distance was 5,

but cheapest distance is 1!

spanning tree
Spanning Tree
  • Subgraphthat is both spanning subgraph& tree
    • Contains all vertices in graph spanning subgraph
    • Tree  connected without any cycles

Graph

spanning tree1
Spanning Tree
  • Subgraphthat is both spanning subgraph& tree
    • Contains all vertices in graph spanning subgraph
    • Tree  connected without any cycles

Tree

spanning tree2
Spanning Tree
  • Subgraphthat is both spanning subgraph& tree
    • Contains all vertices in graph spanning subgraph
    • Tree  connected without any cycles

Spanning subgraph

spanning tree3
Spanning Tree
  • Subgraphthat is both spanning subgraph& tree
    • Contains all vertices in graph spanning subgraph
    • Tree  connected without any cycles

Spanning tree

prim jarnik s algorithm
Prim-Jarnik’s Algorithm
  • Similar to Dijkstra’salgorithm but for MST
  • Processing must start with some vertex s
    • Grow MST using “cloud” of vertices
  • Label vertices with least Edge weight to cloud
  • At each step:
    • Find and add vertex closest to cloud
    • Update adjacent vertices to vertex just added
prim jarnik s algorithm1
Prim-Jarnik’s Algorithm
  • Priority queue stores vertices outside of cloud
    • You all should be reminded of Dijkstra's algorithm
  • Three decorations used for each Vertex
    • Distance from cloud
    • Edge connecting vertex to cloud
    • Entry for Vertexin the priority queue
prim jarnik example
Prim-Jarnik Example

D

7

2

B

4

9

5

F

2

8

C

8

3

8

E

A

7

7

0

prim jarnik example1
Prim-Jarnik Example

D

7

2

B

4

9

5

F

2

8

C

8

3

8

E

A

7

7

0

prim jarnik example2
Prim-Jarnik Example

7

D

7

2

B

4

9

5

F

2

8

C

8

3

8

E

A

7

7

0

prim jarnik example3
Prim-Jarnik Example

7

D

7

2

B

4

9

5

F

2

5

C

8

3

8

E

A

7

7

0

prim jarnik example4
Prim-Jarnik Example

7

D

7

2

B

4

9

5

F

2

5

C

8

3

8

E

A

7

7

0

prim jarnik example5
Prim-Jarnik Example

7

D

7

2

B

4

9

5

F

2

5

C

8

3

8

E

A

7

7

0

prim jarnik example6
Prim-Jarnik Example

7

D

7

2

B

4

9

5

F

2

5

C

8

3

8

E

A

7

7

0

prim jarnik example7
Prim-Jarnik Example

7

D

7

2

B

4

9

5

F

2

5

C

8

3

8

E

A

7

7

0

prim jarnik example8
Prim-Jarnik Example

7

D

7

2

B

4

9

4

5

F

2

5

C

8

3

8

E

A

7

7

0

prim jarnik example9
Prim-Jarnik Example

7

D

7

2

B

4

9

4

5

F

2

5

C

8

3

8

E

A

7

7

0

prim jarnik example10
Prim-Jarnik Example

7

D

7

2

B

4

9

4

5

F

2

5

C

8

3

8

E

A

3

7

0

prim jarnik example11
Prim-Jarnik Example

7

D

7

2

B

4

9

4

5

F

2

5

C

8

3

8

E

A

3

7

0

prim jarnik s analysis
Prim-Jarnik’s Analysis
  • Each connected vertex is:
    • Decorated O(deg(v)) times going through algorithm
    • Priority queue will have added & removed once
  • Takes O((n + m) log n) time using adjacency list
    • Each operation on priority queue takes O(logn) time
    • Takes O(logn) time to decorate Vertex each time
for next lecture
For Next Lecture
  • Weekly assignment available on Angel
    • Due at special time: before next Monday’s quiz
  • Programming assignment #3 designs due Friday
  • Reading on more cheap paths for Friday
    • Why does everything need to be connected?
    • Algorithms for the uptight who do not want to relax?