chapter 14 overview of graph theory and least-cost paths 1 chapter 14 overview of graph theory and...

39
Chapter 14 Overview of Graph Theory and Least-Cost Paths 1 Chapter 1 Chapter 1 4 4 Overview of Graph Theory and Least-Cost Paths

Upload: alisha-phelps

Post on 01-Jan-2016

225 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Chapter 14 Overview of Graph Theory and Least-Cost Paths 1 Chapter 14 Overview of Graph Theory and Least-Cost Paths

Chapter 14 Overview of Graph Theory and Least-Cost Paths1

Chapter 1Chapter 144Overview of Graph Theory and Least-Cost Paths

Page 2: Chapter 14 Overview of Graph Theory and Least-Cost Paths 1 Chapter 14 Overview of Graph Theory and Least-Cost Paths

Chapter 14 Overview of Graph Theory and Least-Cost Paths2

IntroductionIntroduction

Comms networks can be represented by graphs– Switches & routers are vertices– Comms lines are edges

Routing protocols use shortest path algorithms

This chapter is background to chapters on routing

Page 3: Chapter 14 Overview of Graph Theory and Least-Cost Paths 1 Chapter 14 Overview of Graph Theory and Least-Cost Paths

Chapter 14 Overview of Graph Theory and Least-Cost Paths3

Elementary ConceptsElementary Concepts

Graph G(V,E) is two sets of objects– Vertices (or nodes) , set V– Edges, set E

Defined as an unordered pair of vertices

– Shown as dots or circles (vertices) joined by lines (edges)– Vertex i is adjacent to vertex j if (i,j) E– Magnitude of graph G characterised by number of vertices

|V| (called the order of G) and number of edges |E|, size of G

– Running time of algorithm measured in terms of order and size

Page 4: Chapter 14 Overview of Graph Theory and Least-Cost Paths 1 Chapter 14 Overview of Graph Theory and Least-Cost Paths

Chapter 14 Overview of Graph Theory and Least-Cost Paths4

Example GraphExample Graph

Page 5: Chapter 14 Overview of Graph Theory and Least-Cost Paths 1 Chapter 14 Overview of Graph Theory and Least-Cost Paths

Chapter 14 Overview of Graph Theory and Least-Cost Paths5

Adjacent MatrixAdjacent Matrix

Used to represent graph Number vertices

– Arbitrary– 1,2,3,…,|V|

The |V| x |V| adjacent matrix A=(ai,j) defined by: ai,j = 1 if (i,j) E 0 otherwise Matrix symmetrical about upper left to lower

right diagonal– Because edge defined as unordered pair

Page 6: Chapter 14 Overview of Graph Theory and Least-Cost Paths 1 Chapter 14 Overview of Graph Theory and Least-Cost Paths

Chapter 14 Overview of Graph Theory and Least-Cost Paths6

Adjacent Matrix ExampleAdjacent Matrix Example

Page 7: Chapter 14 Overview of Graph Theory and Least-Cost Paths 1 Chapter 14 Overview of Graph Theory and Least-Cost Paths

Chapter 14 Overview of Graph Theory and Least-Cost Paths7

TerminologyTerminology

Two edges incident on same pair of vertices are parallel Edge incident on single vertex is a loop Graph with neither parallel edges nor loos is simple Path from vertex i to vertex j is:

– Alternating sequence of vertices and edge– Starting at i and ending at j– Each edge joins vertices immediately before and after it

Simple path – no vertex nor edge appears more than once In simple graph, simple path may be defined by sequence

of vertices– Each vertex adjacent to preceding and following vertices– No vertex repeated

Page 8: Chapter 14 Overview of Graph Theory and Least-Cost Paths 1 Chapter 14 Overview of Graph Theory and Least-Cost Paths

Chapter 14 Overview of Graph Theory and Least-Cost Paths8

Simple Paths (1)Simple Paths (1)

From V1 to V6 (incomplete list)– V1,V2,V3,V4,V5,V6

– V1,V2,V3,V5,V6

– V1,V2,V3,V6

– V1,V2,V4,V3,V5,V6

– V1,V2,V4,V5,V6

– V1,V3,V2,V4,V5,V6

– V1,V3,V6

– V1,V4,V3,V6

Total of 14 paths (Work out the rest yourself)

Page 9: Chapter 14 Overview of Graph Theory and Least-Cost Paths 1 Chapter 14 Overview of Graph Theory and Least-Cost Paths

Chapter 14 Overview of Graph Theory and Least-Cost Paths9

Simple Paths (2)Simple Paths (2)

V1,V3,V6 is shortest Distance between vertices is minimum number of

edges on all paths Cycle is path staring and ending on same vertex

– E.g. V1,V3,V4,V1

Page 10: Chapter 14 Overview of Graph Theory and Least-Cost Paths 1 Chapter 14 Overview of Graph Theory and Least-Cost Paths

Chapter 14 Overview of Graph Theory and Least-Cost Paths10

DigraphsDigraphs

Directed graph G(V,E) with each edge defined by ordered pair of

vertices Lines, representing edges, have arrow head to indicate

direction Parallel edges allowed if in opposite directions Good for representing comms networks

– Each directed edge represents data flow in one direction Still use adjacent matrix

– Not symmetrical unless each pair of adjacent vertices connected by parallel edges

Page 11: Chapter 14 Overview of Graph Theory and Least-Cost Paths 1 Chapter 14 Overview of Graph Theory and Least-Cost Paths

Chapter 14 Overview of Graph Theory and Least-Cost Paths11

Weighted GraphWeighted Graph

Or weighted digraph Number associated with each edge

– Used to illustrate routing algorithms Adjacent matrix defined as ai,j = wi,j if (i,j) E 0 otherwise

Where wi,j is weight associated with edge (i,j) Length of path is sum of weights Shortest-distance path not necessarily shortest-

length (see next two slides)

Page 12: Chapter 14 Overview of Graph Theory and Least-Cost Paths 1 Chapter 14 Overview of Graph Theory and Least-Cost Paths

Chapter 14 Overview of Graph Theory and Least-Cost Paths12

Weighted Graph and Adjacent Weighted Graph and Adjacent MatrixMatrix

Page 13: Chapter 14 Overview of Graph Theory and Least-Cost Paths 1 Chapter 14 Overview of Graph Theory and Least-Cost Paths

Chapter 14 Overview of Graph Theory and Least-Cost Paths13

Path Distances and Lengths Path Distances and Lengths VV11

to to VV66Path Distance LengthV1,V2,V3,V4,V5,V6 5 11V1,V2,V3,V5,V6 4 8V1,V2,V3,V6 3 10V1,V2,V4,V3,V5,V6 5 10V1,V2,V4,V5,V6 4 7V1,V3,V2,V4,V5,V6 5 16V1,V3,V6 2 10V1,V4,V5,V6 3 4

Page 14: Chapter 14 Overview of Graph Theory and Least-Cost Paths 1 Chapter 14 Overview of Graph Theory and Least-Cost Paths

Chapter 14 Overview of Graph Theory and Least-Cost Paths14

TreesTrees

Subset of graphs Equivalent definitions: Simple graph such that if i and j vertices in T, there is a

unique simple path from i to j Simple graph of N vertices is tree if it has N-1 edges and

no cycles Simple graph of N vertices is tree if it has N-1 edges and

is connected One vertex may be designated root

– Root drawn at top– Vertices adjacent to root drawn at next level

Can reach root on path distance 1

Page 15: Chapter 14 Overview of Graph Theory and Least-Cost Paths 1 Chapter 14 Overview of Graph Theory and Least-Cost Paths

Chapter 14 Overview of Graph Theory and Least-Cost Paths15

Family TreeFamily Tree

Each vertex (except root) has one parent vertex– Adjacent vertex closer to root

Each vertex has zero or more child vertices– Adjacent vertices further from root

– Vertex without children is called a leaf Root assigned level 1

– Vertices immediately under root level 1

– Children of vertices on level 1 are on level 2

Page 16: Chapter 14 Overview of Graph Theory and Least-Cost Paths 1 Chapter 14 Overview of Graph Theory and Least-Cost Paths

Chapter 14 Overview of Graph Theory and Least-Cost Paths16

E.g. TreeE.g. Tree

Page 17: Chapter 14 Overview of Graph Theory and Least-Cost Paths 1 Chapter 14 Overview of Graph Theory and Least-Cost Paths

Chapter 14 Overview of Graph Theory and Least-Cost Paths17

SubgraphSubgraph

Subgraph of graph G obtained by selecting number of edges and vertices from G– For each edge, the two vertices incident on that edge

must be selected Give graph G(E,V), graph G’(E’,V’) is a

subgraph of G iff– V’ V and E’ E and e’ E’, if e’ incident on v’ and w’ then v’, w’ V’

Page 18: Chapter 14 Overview of Graph Theory and Least-Cost Paths 1 Chapter 14 Overview of Graph Theory and Least-Cost Paths

Chapter 14 Overview of Graph Theory and Least-Cost Paths18

Spanning TreeSpanning Tree

Subgraph T of graph G is a spanning tree if– T is a tree– T includes all vertices of G

In other words remove edges from G such that:– Remove all cycles– Maintain connectivity

Not usually unique

Page 19: Chapter 14 Overview of Graph Theory and Least-Cost Paths 1 Chapter 14 Overview of Graph Theory and Least-Cost Paths

Chapter 14 Overview of Graph Theory and Least-Cost Paths19

E.g. Spanning Trees For E.g. Spanning Trees For Previous GraphPrevious Graph

Also previous tree example (slide 16)

Page 20: Chapter 14 Overview of Graph Theory and Least-Cost Paths 1 Chapter 14 Overview of Graph Theory and Least-Cost Paths

Chapter 14 Overview of Graph Theory and Least-Cost Paths20

Breadth First Search (BFS) for Breadth First Search (BFS) for Spanning TreeSpanning Tree Partition vertices of graph into sets at various levels Process all vertices on given level before proceeding to

next level Start at any vertex, x

Assign it level 0– All adjacent vertices are at level 1– Let Vi1, Vi2, Vi3,… Vij, be vertices at level i – Consider all vertices adjacent Vi1 not at level 1,2,…,i

Assign these level (i+1)– Consider all vertices adjacent Vi2 not at level 1,2,3,…,i, (i+1)

Assign these also level (i+1)– Until all vertices processed

Page 21: Chapter 14 Overview of Graph Theory and Least-Cost Paths 1 Chapter 14 Overview of Graph Theory and Least-Cost Paths

Chapter 14 Overview of Graph Theory and Least-Cost Paths21

E.g. Using Previous GraphE.g. Using Previous Graph

Choose order– Obvious one is V1,V2,V3,V4,V5,V6

Select root– Again, obvious one is V1

Let tree T consist of single vertex V1 with no edges Add to T each edge (V1,x) and vertex x

– Such that no cycle is produced– Gives edges (V1,V2), (V1,V3), (V1,V4) and vertices V1,V2, V3

– This is first level Repeat for all level 1 vertices to give level 2

– All vertices now added– If not repeat for level 2 to give level 3 …

Page 22: Chapter 14 Overview of Graph Theory and Least-Cost Paths 1 Chapter 14 Overview of Graph Theory and Least-Cost Paths

Chapter 14 Overview of Graph Theory and Least-Cost Paths22

BFS of Previous GraphBFS of Previous Graph

Page 23: Chapter 14 Overview of Graph Theory and Least-Cost Paths 1 Chapter 14 Overview of Graph Theory and Least-Cost Paths

Chapter 14 Overview of Graph Theory and Least-Cost Paths23

Shortest Path DistanceShortest Path Distance

BFS finds shortest path distance from given source vertex to all other vertices

Minimum number of edges in any path from s to v, δ(s,v)

Page 24: Chapter 14 Overview of Graph Theory and Least-Cost Paths 1 Chapter 14 Overview of Graph Theory and Least-Cost Paths

Chapter 14 Overview of Graph Theory and Least-Cost Paths24

Estimated Running TimeEstimated Running Time

After initialization each vertex is used exactly once as a starting point for adding the next layer– Time take is order of |V|

Each edge already in tree is rejected if examined again

Each edge not in tree is checked to see if it produces a cycle– If not it is included– Bulk of edge processing is once per edge– Time take is order of |E|

Total time taken is linear with |V| and |E|

Page 25: Chapter 14 Overview of Graph Theory and Least-Cost Paths 1 Chapter 14 Overview of Graph Theory and Least-Cost Paths

Chapter 14 Overview of Graph Theory and Least-Cost Paths25

Shorted Path Length Shorted Path Length DeterminationDetermination Packet switching, frame relay or ATM network can be viewed

as digraph– Each node is a vertex– Each link is a pair of parallel edges

For an internet (Internet or intranet)– Each router is vertex– If routers directly connected (e.g. LAN or WAN) two way connection

corresponds to pair of parallel edges– If more than two routers, network represented by multiple pairs of

parallel edges– One pair connecting each pair of routers

In both cases, routing decision needed to pass packet from source to destination– Equivalent to finding path through a graph

Page 26: Chapter 14 Overview of Graph Theory and Least-Cost Paths 1 Chapter 14 Overview of Graph Theory and Least-Cost Paths

Chapter 14 Overview of Graph Theory and Least-Cost Paths26

Routing DecisionsRouting Decisions

Based on least cost– Minimum number of hops

Each edge (hop) has weight 1 Corresponds to minimum path distance

– Or, cost associated with each hop (next slide)

– Cost of path is sum of costs of links in path

– Want least cost path Corresponds to minimum path length in weighted digraph

Page 27: Chapter 14 Overview of Graph Theory and Least-Cost Paths 1 Chapter 14 Overview of Graph Theory and Least-Cost Paths

Chapter 14 Overview of Graph Theory and Least-Cost Paths27

Cost of a HopCost of a Hop

Inversely proportional to path capacity Proportional to current load Monetary cost of link etc. Combination May be different in different directions

Page 28: Chapter 14 Overview of Graph Theory and Least-Cost Paths 1 Chapter 14 Overview of Graph Theory and Least-Cost Paths

Chapter 14 Overview of Graph Theory and Least-Cost Paths28

Dijkstra’s Algorithm (1) –Dijkstra’s Algorithm (1) –DefinitionsDefinitions N = set of vertices in network s = source vertex (starting point) T = set of vertices so far incorporated Tree = spanning tree for vertices in T including edges on

least-cost path from s to each vertex in T w(i,j) = link cost from vertex i to vertex j

– w(i,i) = 0– w(i,j) = if i, j not directly connected by a single edge– w(i,j) 0 of i,j directly connected by single edge

L(n) = cost of least cost path from s to n currently known– At termination, this is least cost path from s to n

Page 29: Chapter 14 Overview of Graph Theory and Least-Cost Paths 1 Chapter 14 Overview of Graph Theory and Least-Cost Paths

Chapter 14 Overview of Graph Theory and Least-Cost Paths29

Dijkstra’s Algorithm (2) –Dijkstra’s Algorithm (2) –StepsSteps1. Initialization

a. T = Tree = {s} - only source is so far incorporatedb. L(n) = w(s,n) for n s - initial path cost to neighbors are link

costs

2. Get next vertexa. Find x T such that L(x) = min L(j), j Tb. Add x to T and Treec. Add edge to T incident on x and has least cost

Last hop in path

3. Update least cost pathsa. L(n) = min[L(n), L(x) + w(x,n)] n T

If latter term is minimum, path from s to n is now path from s to x concatenated with edge from x to n

Page 30: Chapter 14 Overview of Graph Theory and Least-Cost Paths 1 Chapter 14 Overview of Graph Theory and Least-Cost Paths

Chapter 14 Overview of Graph Theory and Least-Cost Paths30

Dijkstra’s Algorithm (3) –Dijkstra’s Algorithm (3) –NotesNotes Terminate when all vertices added to T Requires |V| iterations At termination

– L(x) associated with each vertex is cost of least cost path from s to x

– Tree is a spanning tree Defines least cost path from s to each other vertex

One step adds one vertex to T and defines least cost path from s to that vertex

Running time order of |V|2

Page 31: Chapter 14 Overview of Graph Theory and Least-Cost Paths 1 Chapter 14 Overview of Graph Theory and Least-Cost Paths

Chapter 14 Overview of Graph Theory and Least-Cost Paths31

Dijkstra’s Algorithm on Dijkstra’s Algorithm on Example GraphExample Graph

Page 32: Chapter 14 Overview of Graph Theory and Least-Cost Paths 1 Chapter 14 Overview of Graph Theory and Least-Cost Paths

Chapter 14 Overview of Graph Theory and Least-Cost Paths32

Bellman-Ford Algorithm (1) –Bellman-Ford Algorithm (1) –DefinitionsDefinitions s = source vertex (starting point) w(i,j) = link cost from vertex i to vertex j

– w(i,i) = 0

– w(i,j) = if i, j not directly connected by a single edge

– w(i,j) 0 of i,j directly connected by single edge h = max number of links in path at current stage Lh(n) = cost of least cost path from s to n such

that no more than h links

Page 33: Chapter 14 Overview of Graph Theory and Least-Cost Paths 1 Chapter 14 Overview of Graph Theory and Least-Cost Paths

Chapter 14 Overview of Graph Theory and Least-Cost Paths33

Bellman-Ford Algorithm (2) –Bellman-Ford Algorithm (2) –StepsSteps1. Initialization

a. L0(n) = n s

b. Lh(s) = 0 h

2. Updatea. For each successive h 0

i. For each n s, compute:

Lh+1+(n) = min[Lh(j)+ w(j,n)], jii. Connect n with predecessor vertex j that achieves minimumiii. Eliminate any connection of n with different predecessor

vertex from previous iterationiv. Path from s to n terminates with link from j to n

Page 34: Chapter 14 Overview of Graph Theory and Least-Cost Paths 1 Chapter 14 Overview of Graph Theory and Least-Cost Paths

Chapter 14 Overview of Graph Theory and Least-Cost Paths34

Bellman-Ford Algorithm (3) –Bellman-Ford Algorithm (3) –NotesNotesResults agree with DijkstraRunning time order of |V| x |E|

Page 35: Chapter 14 Overview of Graph Theory and Least-Cost Paths 1 Chapter 14 Overview of Graph Theory and Least-Cost Paths

Chapter 14 Overview of Graph Theory and Least-Cost Paths35

Bellman-Ford Algorithm on Bellman-Ford Algorithm on Example GraphExample Graph

Page 36: Chapter 14 Overview of Graph Theory and Least-Cost Paths 1 Chapter 14 Overview of Graph Theory and Least-Cost Paths

Chapter 14 Overview of Graph Theory and Least-Cost Paths36

Results of Dijkstra and Results of Dijkstra and Bellman-FordBellman-Ford

Page 37: Chapter 14 Overview of Graph Theory and Least-Cost Paths 1 Chapter 14 Overview of Graph Theory and Least-Cost Paths

Chapter 14 Overview of Graph Theory and Least-Cost Paths37

ComparisonComparison of Information of Information Needed – Bellman-FordNeeded – Bellman-Ford Calculation for vertex n involves knowledge of

link cost to all neighbors of n plus total path cost to each from source

Each vertex can keep set of costs and paths for every other vertex in network

Exchange information with direct neighbors Each vertex can use use Bellman-Ford step 2

based on information from neighbors and knowledge of link costs to update its costs and paths

Page 38: Chapter 14 Overview of Graph Theory and Least-Cost Paths 1 Chapter 14 Overview of Graph Theory and Least-Cost Paths

Chapter 14 Overview of Graph Theory and Least-Cost Paths38

ComparisonComparison of Information of Information Needed – DijkstraNeeded – DijkstraStep 3 requires each vertex must have

complete topology– Must know link costs of all links in network– Information must be exchanged between all

other verticesEvaluation must also consider calculation

time

Page 39: Chapter 14 Overview of Graph Theory and Least-Cost Paths 1 Chapter 14 Overview of Graph Theory and Least-Cost Paths

Chapter 14 Overview of Graph Theory and Least-Cost Paths39

Other NotesOther Notes

Both Algorithms converge under static conditions of topology and link cost

Give to same solutionIf link costs change, algorithms will

attempt to catch upIf link costs depend on traffic, which

depends on routes chosen:– Feedback condition exists– Instability may result