graphs, networks and algorithms - shortest path...

47
ECM3722 Barrie Cooper Shortest path problems Greedy routing Dijkstra’s algorithm Complexity Connectivity Drawbacks 1 Graphs, Networks and Algorithms Shortest Path Algorithms April 30, 2009 Barrie Cooper Room 161, Harrison [email protected]

Upload: others

Post on 16-May-2020

26 views

Category:

Documents


0 download

TRANSCRIPT

ECM3722

Barrie Cooper

Shortest path

problems

Greedy routing

Dijkstra’s

algorithm

Complexity

Connectivity

Drawbacks

1

Graphs, Networks and AlgorithmsShortest Path Algorithms

April 30, 2009

Barrie CooperRoom 161, Harrison

[email protected]

ECM3722

Barrie Cooper

Shortest path

problems

Greedy routing

Dijkstra’s

algorithm

Complexity

Connectivity

Drawbacks

2

Shortest path problems

Many problems call for the shortest path to be identifiedbetween two points of a graph.

ECM3722

Barrie Cooper

Shortest path

problems

Greedy routing

Dijkstra’s

algorithm

Complexity

Connectivity

Drawbacks

2

Shortest path problems

Many problems call for the shortest path to be identifiedbetween two points of a graph.

• The most obvious of these is route-planning, where wewant to get from A to B by the shortest route or in theshortest amount of time,

ECM3722

Barrie Cooper

Shortest path

problems

Greedy routing

Dijkstra’s

algorithm

Complexity

Connectivity

Drawbacks

2

Shortest path problems

Many problems call for the shortest path to be identifiedbetween two points of a graph.

• The most obvious of these is route-planning, where wewant to get from A to B by the shortest route or in theshortest amount of time,

but other unrelated problems can be transformed into a”shortest path problem”, such as:

ECM3722

Barrie Cooper

Shortest path

problems

Greedy routing

Dijkstra’s

algorithm

Complexity

Connectivity

Drawbacks

2

Shortest path problems

Many problems call for the shortest path to be identifiedbetween two points of a graph.

• The most obvious of these is route-planning, where wewant to get from A to B by the shortest route or in theshortest amount of time,

but other unrelated problems can be transformed into a”shortest path problem”, such as:

• packing the boot of a car;

ECM3722

Barrie Cooper

Shortest path

problems

Greedy routing

Dijkstra’s

algorithm

Complexity

Connectivity

Drawbacks

2

Shortest path problems

Many problems call for the shortest path to be identifiedbetween two points of a graph.

• The most obvious of these is route-planning, where wewant to get from A to B by the shortest route or in theshortest amount of time,

but other unrelated problems can be transformed into a”shortest path problem”, such as:

• packing the boot of a car;

• production planning and scheduling.

ECM3722

Barrie Cooper

Shortest path

problems

Greedy routing

Dijkstra’s

algorithm

Complexity

Connectivity

Drawbacks

3

A big business

Finding solutions to path-finding problems is big business:

ECM3722

Barrie Cooper

Shortest path

problems

Greedy routing

Dijkstra’s

algorithm

Complexity

Connectivity

Drawbacks

3

A big business

Finding solutions to path-finding problems is big business:

• In the tax year 2006/7, sales of satellite navigationsystems reached £340m;

ECM3722

Barrie Cooper

Shortest path

problems

Greedy routing

Dijkstra’s

algorithm

Complexity

Connectivity

Drawbacks

3

A big business

Finding solutions to path-finding problems is big business:

• In the tax year 2006/7, sales of satellite navigationsystems reached £340m;

• double the figure in 2004

ECM3722

Barrie Cooper

Shortest path

problems

Greedy routing

Dijkstra’s

algorithm

Complexity

Connectivity

Drawbacks

3

A big business

Finding solutions to path-finding problems is big business:

• In the tax year 2006/7, sales of satellite navigationsystems reached £340m;

• double the figure in 2004

• and a huge increase on the £5m spent in 2002.

ECM3722

Barrie Cooper

Shortest path

problems

Greedy routing

Dijkstra’s

algorithm

Complexity

Connectivity

Drawbacks

3

A big business

Finding solutions to path-finding problems is big business:

• In the tax year 2006/7, sales of satellite navigationsystems reached £340m;

• double the figure in 2004

• and a huge increase on the £5m spent in 2002.

Companies also spend a lot of money on effective projectmanagement, part of which involves planning andcontinually reviewing a project in order to complete it asefficiently as possible.

ECM3722

Barrie Cooper

Shortest path

problems

Greedy routing

Dijkstra’s

algorithm

Complexity

Connectivity

Drawbacks

3

A big business

Finding solutions to path-finding problems is big business:

• In the tax year 2006/7, sales of satellite navigationsystems reached £340m;

• double the figure in 2004

• and a huge increase on the £5m spent in 2002.

Companies also spend a lot of money on effective projectmanagement, part of which involves planning andcontinually reviewing a project in order to complete it asefficiently as possible.

The precise algorithms used in finding such solutions areoften closely guarded trade secrets.

ECM3722

Barrie Cooper

Shortest path

problems

Greedy routing

Dijkstra’s

algorithm

Complexity

Connectivity

Drawbacks

4

A heuristic approach

Often, in simple everyday situations we naturally use aheuristic approach.

ECM3722

Barrie Cooper

Shortest path

problems

Greedy routing

Dijkstra’s

algorithm

Complexity

Connectivity

Drawbacks

4

A heuristic approach

Often, in simple everyday situations we naturally use aheuristic approach.

Suppose we are travelling across the London Underground.

ECM3722

Barrie Cooper

Shortest path

problems

Greedy routing

Dijkstra’s

algorithm

Complexity

Connectivity

Drawbacks

4

A heuristic approach

Often, in simple everyday situations we naturally use aheuristic approach.

Suppose we are travelling across the London Underground.

A common and quick method for planning travel is to findout which tube line leaving our current station goes closestto our destination.

ECM3722

Barrie Cooper

Shortest path

problems

Greedy routing

Dijkstra’s

algorithm

Complexity

Connectivity

Drawbacks

4

A heuristic approach

Often, in simple everyday situations we naturally use aheuristic approach.

Suppose we are travelling across the London Underground.

A common and quick method for planning travel is to findout which tube line leaving our current station goes closestto our destination.

This may not be the quickest method of travel (for exampleit may be worth going a couple of stations in the “wrong”direction to take a more direct train) but we are instinctivelysuspicious of routes that take us further away from ourdestination.

ECM3722

Barrie Cooper

Shortest path

problems

Greedy routing

Dijkstra’s

algorithm

Complexity

Connectivity

Drawbacks

5

Greedy routing

This strategy for navigating the London Underground is anexample of a greedy routing algorithm:

ECM3722

Barrie Cooper

Shortest path

problems

Greedy routing

Dijkstra’s

algorithm

Complexity

Connectivity

Drawbacks

5

Greedy routing

This strategy for navigating the London Underground is anexample of a greedy routing algorithm:

“Get as close to the destination as possible with

each step.”

ECM3722

Barrie Cooper

Shortest path

problems

Greedy routing

Dijkstra’s

algorithm

Complexity

Connectivity

Drawbacks

5

Greedy routing

This strategy for navigating the London Underground is anexample of a greedy routing algorithm:

“Get as close to the destination as possible with

each step.”

Greedy routing algorithms can be powerful and efficient if

ECM3722

Barrie Cooper

Shortest path

problems

Greedy routing

Dijkstra’s

algorithm

Complexity

Connectivity

Drawbacks

5

Greedy routing

This strategy for navigating the London Underground is anexample of a greedy routing algorithm:

“Get as close to the destination as possible with

each step.”

Greedy routing algorithms can be powerful and efficient if

• there is an “as the crow flies” notion of distancebetween vertices;

ECM3722

Barrie Cooper

Shortest path

problems

Greedy routing

Dijkstra’s

algorithm

Complexity

Connectivity

Drawbacks

5

Greedy routing

This strategy for navigating the London Underground is anexample of a greedy routing algorithm:

“Get as close to the destination as possible with

each step.”

Greedy routing algorithms can be powerful and efficient if

• there is an “as the crow flies” notion of distancebetween vertices;

• the network is well-connected;

ECM3722

Barrie Cooper

Shortest path

problems

Greedy routing

Dijkstra’s

algorithm

Complexity

Connectivity

Drawbacks

5

Greedy routing

This strategy for navigating the London Underground is anexample of a greedy routing algorithm:

“Get as close to the destination as possible with

each step.”

Greedy routing algorithms can be powerful and efficient if

• there is an “as the crow flies” notion of distancebetween vertices;

• the network is well-connected;

• getting close to your destination is acceptable if thingsgo wrong (e.g. in Central London even stations ondifferent lines are often no more than 10 minutes walkfrom each other).

ECM3722

Barrie Cooper

Shortest path

problems

Greedy routing

Dijkstra’s

algorithm

Complexity

Connectivity

Drawbacks

6

Problems with being greedy

But being greedy rarely finds the best solution and can oftenstore up problems for future decisions.

ECM3722

Barrie Cooper

Shortest path

problems

Greedy routing

Dijkstra’s

algorithm

Complexity

Connectivity

Drawbacks

6

Problems with being greedy

But being greedy rarely finds the best solution and can oftenstore up problems for future decisions.

Satellite navigation systems will use greedy algorithms inpart, because when driving you need an instant decision

ECM3722

Barrie Cooper

Shortest path

problems

Greedy routing

Dijkstra’s

algorithm

Complexity

Connectivity

Drawbacks

6

Problems with being greedy

But being greedy rarely finds the best solution and can oftenstore up problems for future decisions.

Satellite navigation systems will use greedy algorithms inpart, because when driving you need an instant decision

but. . .

ECM3722

Barrie Cooper

Shortest path

problems

Greedy routing

Dijkstra’s

algorithm

Complexity

Connectivity

Drawbacks

6

Problems with being greedy

But being greedy rarely finds the best solution and can oftenstore up problems for future decisions.

Satellite navigation systems will use greedy algorithms inpart, because when driving you need an instant decision

but. . .

we have all heard stories of a satellite navigation systemleading someone to a farm house a short distance from the‘intended’ destination.

ECM3722

Barrie Cooper

Shortest path

problems

Greedy routing

Dijkstra’s

algorithm

Complexity

Connectivity

Drawbacks

7

An exact method

There are a number of exact methods for finding theshortest path through a network.

ECM3722

Barrie Cooper

Shortest path

problems

Greedy routing

Dijkstra’s

algorithm

Complexity

Connectivity

Drawbacks

7

An exact method

There are a number of exact methods for finding theshortest path through a network.

Suppose we wish to travel from our source vertex s to ourdestination vertex t.

ECM3722

Barrie Cooper

Shortest path

problems

Greedy routing

Dijkstra’s

algorithm

Complexity

Connectivity

Drawbacks

7

An exact method

There are a number of exact methods for finding theshortest path through a network.

Suppose we wish to travel from our source vertex s to ourdestination vertex t.

Then

• the shortest path from s to t must consist of theshortest path from s to any intermediate vertex v onthe route followed by the shortest path from v to t.

ECM3722

Barrie Cooper

Shortest path

problems

Greedy routing

Dijkstra’s

algorithm

Complexity

Connectivity

Drawbacks

7

An exact method

There are a number of exact methods for finding theshortest path through a network.

Suppose we wish to travel from our source vertex s to ourdestination vertex t.

Then

• the shortest path from s to t must consist of theshortest path from s to any intermediate vertex v onthe route followed by the shortest path from v to t.

This observation forms the basis of Dijkstra’s algorithm.

ECM3722

Barrie Cooper

Shortest path

problems

Greedy routing

Dijkstra’s

algorithm

Complexity

Connectivity

Drawbacks

8

Dijkstra’s algorithm

1: Assign a temporary label li = ∞ to all vertices in thenetwork except vertex s, for which ls = 0.

ECM3722

Barrie Cooper

Shortest path

problems

Greedy routing

Dijkstra’s

algorithm

Complexity

Connectivity

Drawbacks

8

Dijkstra’s algorithm

1: Assign a temporary label li = ∞ to all vertices in thenetwork except vertex s, for which ls = 0.

2: Find the vertex k which has a temporary label and forwhich the label value lk is smallest. (Choose arbitrarilyin the case of ties unless the tie includes the vertex t, inwhich case t should be chosen.) Make lk permanentand call it Lk. If k = t then stop as the length of theshortest path has been found; the shortest path from s

to t is the set of labelled feeder edges, working backfrom t.

ECM3722

Barrie Cooper

Shortest path

problems

Greedy routing

Dijkstra’s

algorithm

Complexity

Connectivity

Drawbacks

8

Dijkstra’s algorithm

1: Assign a temporary label li = ∞ to all vertices in thenetwork except vertex s, for which ls = 0.

2: Find the vertex k which has a temporary label and forwhich the label value lk is smallest. (Choose arbitrarilyin the case of ties unless the tie includes the vertex t, inwhich case t should be chosen.) Make lk permanentand call it Lk. If k = t then stop as the length of theshortest path has been found; the shortest path from s

to t is the set of labelled feeder edges, working backfrom t.

3: For every vertex j with a temporary label, determine thesmaller of lj and Lk + dkj (where dkj is the length ofthe shortest edge from k to j) and assign this value tothe label lj. If there are temporary labels that are notinfinite then return to step 2; otherwise go to step 4.

ECM3722

Barrie Cooper

Shortest path

problems

Greedy routing

Dijkstra’s

algorithm

Complexity

Connectivity

Drawbacks

8

Dijkstra’s algorithm

1: Assign a temporary label li = ∞ to all vertices in thenetwork except vertex s, for which ls = 0.

2: Find the vertex k which has a temporary label and forwhich the label value lk is smallest. (Choose arbitrarilyin the case of ties unless the tie includes the vertex t, inwhich case t should be chosen.) Make lk permanentand call it Lk. If k = t then stop as the length of theshortest path has been found; the shortest path from s

to t is the set of labelled feeder edges, working backfrom t.

3: For every vertex j with a temporary label, determine thesmaller of lj and Lk + dkj (where dkj is the length ofthe shortest edge from k to j) and assign this value tothe label lj. If there are temporary labels that are notinfinite then return to step 2; otherwise go to step 4.

4: The vertices with temporary labels all have labels of ∞,so the algorithm must stop; there is no path from s to t.

ECM3722

Barrie Cooper

Shortest path

problems

Greedy routing

Dijkstra’s

algorithm

Complexity

Connectivity

Drawbacks

9

Complexity

On a simple graph, Dijkstra’s algorithm has complexityO(|V |2):

ECM3722

Barrie Cooper

Shortest path

problems

Greedy routing

Dijkstra’s

algorithm

Complexity

Connectivity

Drawbacks

9

Complexity

On a simple graph, Dijkstra’s algorithm has complexityO(|V |2):

• with each iteration, a new vertex label is fixedpermanently, so there are at most |V | iterationsrequired;

ECM3722

Barrie Cooper

Shortest path

problems

Greedy routing

Dijkstra’s

algorithm

Complexity

Connectivity

Drawbacks

9

Complexity

On a simple graph, Dijkstra’s algorithm has complexityO(|V |2):

• with each iteration, a new vertex label is fixedpermanently, so there are at most |V | iterationsrequired;

• within each iteration we need to

ECM3722

Barrie Cooper

Shortest path

problems

Greedy routing

Dijkstra’s

algorithm

Complexity

Connectivity

Drawbacks

9

Complexity

On a simple graph, Dijkstra’s algorithm has complexityO(|V |2):

• with each iteration, a new vertex label is fixedpermanently, so there are at most |V | iterationsrequired;

• within each iteration we need to

- find the vertex with smallest temporary label and makeit permanent, which is O(|V |), and

ECM3722

Barrie Cooper

Shortest path

problems

Greedy routing

Dijkstra’s

algorithm

Complexity

Connectivity

Drawbacks

9

Complexity

On a simple graph, Dijkstra’s algorithm has complexityO(|V |2):

• with each iteration, a new vertex label is fixedpermanently, so there are at most |V | iterationsrequired;

• within each iteration we need to

- find the vertex with smallest temporary label and makeit permanent, which is O(|V |), and

- compute the new vertex labels from this vertex, which isalso O(|V |).

ECM3722

Barrie Cooper

Shortest path

problems

Greedy routing

Dijkstra’s

algorithm

Complexity

Connectivity

Drawbacks

9

Complexity

On a simple graph, Dijkstra’s algorithm has complexityO(|V |2):

• with each iteration, a new vertex label is fixedpermanently, so there are at most |V | iterationsrequired;

• within each iteration we need to

- find the vertex with smallest temporary label and makeit permanent, which is O(|V |), and

- compute the new vertex labels from this vertex, which isalso O(|V |).

For a graph with multiple edges between vertices, we couldsort the edges first and throw out all but the smallest edgebetween each pair of vertices. This has complexityO(|E| log|E|), which will dominate for well-connectedgraphs.

ECM3722

Barrie Cooper

Shortest path

problems

Greedy routing

Dijkstra’s

algorithm

Complexity

Connectivity

Drawbacks

10

Dijkstra’s algorithm can also tell us if our graph is connected:

ECM3722

Barrie Cooper

Shortest path

problems

Greedy routing

Dijkstra’s

algorithm

Complexity

Connectivity

Drawbacks

10

Dijkstra’s algorithm can also tell us if our graph is connected:

• choose any vertex s as the starting point;

ECM3722

Barrie Cooper

Shortest path

problems

Greedy routing

Dijkstra’s

algorithm

Complexity

Connectivity

Drawbacks

10

Dijkstra’s algorithm can also tell us if our graph is connected:

• choose any vertex s as the starting point;

• find the shortest path from s to every vertex usingDijkstra’s algorithm;

ECM3722

Barrie Cooper

Shortest path

problems

Greedy routing

Dijkstra’s

algorithm

Complexity

Connectivity

Drawbacks

10

Dijkstra’s algorithm can also tell us if our graph is connected:

• choose any vertex s as the starting point;

• find the shortest path from s to every vertex usingDijkstra’s algorithm;

• if any labels are infinite upon termination of thealgorithm then there can be no path from s to thatvertex, that is, the graph is disconnected.

ECM3722

Barrie Cooper

Shortest path

problems

Greedy routing

Dijkstra’s

algorithm

Complexity

Connectivity

Drawbacks

11

Drawbacks

Dijkstra’s algorithm cannot be used with negative weights(e.g. weights could represent profit or loss for a course ofaction).

ECM3722

Barrie Cooper

Shortest path

problems

Greedy routing

Dijkstra’s

algorithm

Complexity

Connectivity

Drawbacks

11

Drawbacks

Dijkstra’s algorithm cannot be used with negative weights(e.g. weights could represent profit or loss for a course ofaction).

This is because Dijkstra’s algorithm is still, in essence,greedy. Once a label is fixed it cannot be changed, soDijkstra’s algorithm will always choose profit over loss, evenif a greater profit can be made by incurring the loss now.

ECM3722

Barrie Cooper

Shortest path

problems

Greedy routing

Dijkstra’s

algorithm

Complexity

Connectivity

Drawbacks

11

Drawbacks

Dijkstra’s algorithm cannot be used with negative weights(e.g. weights could represent profit or loss for a course ofaction).

This is because Dijkstra’s algorithm is still, in essence,greedy. Once a label is fixed it cannot be changed, soDijkstra’s algorithm will always choose profit over loss, evenif a greater profit can be made by incurring the loss now.

Modifications can be made to Dijkstra’s algorithm toincorporate such possibilities. One such modification isFord’s algorithm.