graphs part ii lecture 7. lecture objectives topological sort spanning tree minimum spanning tree...

48
Graphs Part II Graphs Part II Lecture 7 Lecture 7

Upload: alan-cobb

Post on 19-Jan-2016

226 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path

Graphs Part IIGraphs Part II

Lecture 7Lecture 7

Page 2: Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path

Lecture ObjectivesLecture Objectives

Topological SortTopological Sort Spanning TreeSpanning Tree Minimum Spanning TreeMinimum Spanning Tree Shortest PathShortest Path

Page 3: Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path

Applications of GraphApplications of Graph

There are many useful applications of graphs.There are many useful applications of graphs. Some common applications are as followsSome common applications are as follows

Topological Sort

Spanning Tree

Minimum Spanning Tree

Shortest Path

Traveling Salesman Problem

Three Utilities Problem

Graph Coloring Problem

Page 4: Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path

Topological SortTopological Sort A directed graph without cycles has a natural order.A directed graph without cycles has a natural order. ExampleExample : : Vertex a precedes vertex b which precedes vertex cVertex a precedes vertex b which precedes vertex c..

There is a linear order, called There is a linear order, called topological ordertopological order of the vertices in of the vertices in the directed graph without cyclesthe directed graph without cycles

In the list of vertices in In the list of vertices in topological ordertopological order vertex x vertex x precedes vertex yprecedes vertex y if there is a if there is a directeddirected edge from x to y in the edge from x to y in the graphgraph

If the graph represent academic courses then the graph represents the If the graph represent academic courses then the graph represents the prerequisite structure for the courses. Course a is the prerequisite for prerequisite structure for the courses. Course a is the prerequisite for course b which is the prerequisite for c and e.course b which is the prerequisite for c and e.

GraphGraph

Page 5: Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path

The vertex in the graph may have several topological orders.The vertex in the graph may have several topological orders. Example :Example : a,g,d,b,e,c,fa,g,d,b,e,c,f and and a,b,g,d,e,f,ca,b,g,d,e,f,cIf you arrange the vertices of a directed graph linearly and in If you arrange the vertices of a directed graph linearly and in topological order, the edges will point in one directiontopological order, the edges will point in one direction

Topological SortTopological Sort

GraphGraph

Arranging the vertices into a topological order is called Arranging the vertices into a topological order is called topological sorting.topological sorting.

Page 6: Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path

Simple algorithm for top sort1Simple algorithm for top sort1

WorkingWorking : :1.1. Find a vertex which has no successor.Find a vertex which has no successor.2.2. Remove it from the graph and all edges that Remove it from the graph and all edges that

lead to it.lead to it.3.3. Add it to the beginning of the list of verticesAdd it to the beginning of the list of vertices4.4. Do the same for the remaining vertices in Do the same for the remaining vertices in

the graph.the graph.5.5. ResultResult : When the graph is empty the list of : When the graph is empty the list of

vertices will be in topological order.vertices will be in topological order.

Page 7: Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path

1 . 1 . N = number of vertices in the N = number of vertices in the graphgraph

2. 2. For ( step = 1 through n)For ( step = 1 through n){{

select a vertex v that has no select a vertex v that has no

successorssuccessorsaList.insert (1,v)aList.insert (1,v)Delete from the graph vertex Delete from the graph vertex

v and its edgesv and its edges}}

When the traversal ends the list of When the traversal ends the list of vertices will be in topological ordervertices will be in topological order..

Simple algorithm for top sort1Simple algorithm for top sort1

Page 8: Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path

Simple algorithm for top sort1Simple algorithm for top sort1

Page 9: Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path

Spanning TreeSpanning Tree A Tree is a special kind of graph with no cycles.A Tree is a special kind of graph with no cycles. All trees are graphs but not all graphs are treesAll trees are graphs but not all graphs are trees The nodes of a tree have a hierarchical The nodes of a tree have a hierarchical

arrangement that is not required of all graphsarrangement that is not required of all graphs A spanning tree of a connected undirected graph A spanning tree of a connected undirected graph

G is a sub graph of G that contains all vertices of G is a sub graph of G that contains all vertices of G and enough edges to form a tree.G and enough edges to form a tree.

GraphGraph Spanning TreeSpanning Tree

Page 10: Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path

Spanning TreeSpanning Tree

There may be several spanning tree for a given graphThere may be several spanning tree for a given graph

WorkingWorking : :

If you have a connected undirected graph with cycles and you If you have a connected undirected graph with cycles and you remove edges until there are no cycles , then you will obtain a remove edges until there are no cycles , then you will obtain a spanning tree for the graph spanning tree for the graph

Observation about an undirected graph to detect a cycleObservation about an undirected graph to detect a cycle

A connected undirected graph that has n vertices must have at A connected undirected graph that has n vertices must have at least n-1 edgesleast n-1 edges

A connected undirected graph that has n vertices and exactly n-1 A connected undirected graph that has n vertices and exactly n-1 edges cannot contain a cycleedges cannot contain a cycle

A connected undirected graph that has n vertices and more than n-1 A connected undirected graph that has n vertices and more than n-1 edges must contain at least one cycleedges must contain at least one cycle

Page 11: Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path

AlgorithmsAlgorithms Two algorithms are used for determining a spanning Two algorithms are used for determining a spanning

tree of graph .tree of graph . These algorithms produce different spanning trees for These algorithms produce different spanning trees for

any particular graph.any particular graph.

1. DFS Spanning Tree1. DFS Spanning Tree 2. BFS Spanning Tree2. BFS Spanning Tree

One way to determine the One way to determine the spanning tree is to traverse spanning tree is to traverse the graph's vertices by using the graph's vertices by using a Depth first search.a Depth first search.

After the traversal the After the traversal the graph’s vertices and the graph’s vertices and the marked edges form the marked edges form the spanning tree called DFS spanning tree called DFS spanning treespanning tree

Another way to determine the Another way to determine the spanning tree is to traverse the spanning tree is to traverse the graph's vertices by using a graph's vertices by using a Breadth first search.Breadth first search.

After the traversal the graph’s After the traversal the graph’s vertices and the marked edges vertices and the marked edges form the spanning tree called form the spanning tree called BFS spanning treeBFS spanning tree

Page 12: Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path

DFS Spanning TreeDFS Spanning Tree

Dfstree(in v:vertex)Dfstree(in v:vertex)Mark v as visitedMark v as visitedfor ( each unvisited vertex u for ( each unvisited vertex u

adjacent to v )adjacent to v ){{

Mark the edge from u to vMark the edge from u to vDfstree(u)Dfstree(u)

}}After DFSAfter DFS

Page 13: Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path

BFS Spanning TreeBFS Spanning Tree

Bfstree(in v:vertex)Bfstree(in v:vertex)q.createQueue()q.createQueue()q.enqueue(v)q.enqueue(v)Mark v as visitedMark v as visitedwhile (!q.empty())while (!q.empty()){{ q.dequeue(w)q.dequeue(w) for (for (each unvisited vertex u adjacent to w)each unvisited vertex u adjacent to w)

{{ Mark u as visited Mark u as visited Mark edge between w and u Mark edge between w and u

q.enqueue(u)q.enqueue(u)}}

}}

After BFSAfter BFS

Page 14: Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path

Minimum Spanning TreesMinimum Spanning Trees

ObjectiveObjective : To minimize the length of a connecting : To minimize the length of a connecting network network

The resulting connection graph is connected, The resulting connection graph is connected, undirected, and acyclic, i.e., aundirected, and acyclic, i.e., a free treefree tree (sometimes (sometimes called simply a called simply a treetree).).

So a minimum spanning tree (MST) is a spanning tree of minimum weight (cost).

Page 15: Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path

ExampleExample

Graph

CostCost of the spanning of the spanning tree is the sum of the tree is the sum of the cost of the edges in the cost of the edges in the spanning treespanning tree

Selecting the Selecting the least cost least cost spanning treespanning tree is is minimum cost spanning minimum cost spanning treetree

Page 16: Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path

AlgorithmsAlgorithms

Two algorithms for computing MSTs:Two algorithms for computing MSTs:– Kruskal’s AlgorithmKruskal’s Algorithm

– Prim’s Algorithm Prim’s Algorithm • Both are variants of the same generic approach.Both are variants of the same generic approach.

Page 17: Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path

Kruskal’s AlgorithmKruskal’s Algorithm

Working :Working :

1.1. If the next edge does not induce a cycle among the current set If the next edge does not induce a cycle among the current set of edges, then it is added toof edges, then it is added to A. A.

2.2. If it does, then this edge is passed over, and we consider the If it does, then this edge is passed over, and we consider the next edge in order.next edge in order.

3.3. As this algorithm runs, the edges of As this algorithm runs, the edges of AA will induce a forest on the will induce a forest on the vertices and the trees of this forest are merged together until we vertices and the trees of this forest are merged together until we have a single tree containing all vertices.have a single tree containing all vertices.

Kruskal attempts to add edges to Kruskal attempts to add edges to A A in increasing order of in increasing order of weight (lightest edge first).weight (lightest edge first).

Image next slideImage next slide

Page 18: Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path

Kruskal’s AlgorithmKruskal’s Algorithm

Page 19: Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path

Prim’s AlgorithmPrim’s Algorithm Prim works by adding leaves one at a time to the current tree. Prim works by adding leaves one at a time to the current tree.

Working :

1. Start with the root vertex r (it can be any vertex).

2. At each step select , a light edge connecting a vertex in tree to a vertex not in tree

3. The tree grows until it spans all the vertices

Page 20: Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path

Primsalgorithm(in v:vertex)Primsalgorithm(in v:vertex)

Mark vertex v as visited and include it in the Mark vertex v as visited and include it in the minimum spanning treeminimum spanning tree

while ( there are unvisited vertices)while ( there are unvisited vertices)

{ {

find the least cost edge (v,u) from a visited find the least cost edge (v,u) from a visited vertex vertex v to some unvisited vertex uv to some unvisited vertex u

Mark u as visitedMark u as visited

Add the vertex u and the edge (v,u) to the Add the vertex u and the edge (v,u) to the minimum spanning treeminimum spanning tree

}}

Prim’s AlgorithmPrim’s Algorithm

Trace of this algorithm - Next Slide

Page 21: Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path

Prim’s AlgorithmPrim’s Algorithm GraphGraph

StepsSteps

Not yet complete... Leave it as exercise

Page 22: Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path

Shortest Path ProblemShortest Path Problem

We are given a graph We are given a graph GG = ( = (VV, , EE) with weight function ) with weight function ww The The weightweight of a path of a path pp = < = <vv00, , vv11, …, , …, vvkk> is the sum of > is the sum of

edge weights along the path.edge weights along the path. The The shortest-pathshortest-path weightweight between two vertices, between two vertices, uu and and vv, ,

is the length of the minimum weight path from is the length of the minimum weight path from uu to to vv

Given a directed graph Given a directed graph G = G = ((V, EV, E) with edge weights and ) with edge weights and a distinguisheda distinguished source vertexsource vertex, , determine the distance from determine the distance from the source vertex to every the source vertex to every vertex in the graph.vertex in the graph.

Single-Source Shortest PathsSingle-Source Shortest Paths

Page 23: Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path

Single-Source Shortest PathsSingle-Source Shortest Paths

Edge weights can be negative; but in order for the Edge weights can be negative; but in order for the problem to be well-defined there problem to be well-defined there mustmust be no cycle be no cycle whose total cost is negative.whose total cost is negative. Why?Why?

In general, we assume there are no cycles at all:In general, we assume there are no cycles at all:

– Zero-weight cycles clearly can be removed as they do Zero-weight cycles clearly can be removed as they do not affect total path weights.not affect total path weights.

– Negative cycles must be removed.Negative cycles must be removed.– Removing positive-weight cycles from a path will Removing positive-weight cycles from a path will

reduce the weight of the path, so they also should be reduce the weight of the path, so they also should be removed.removed.

Page 24: Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path

VariantsVariants

Single-destination shortest-paths problemSingle-destination shortest-paths problem: Find a : Find a shortest path to a given destination vertex shortest path to a given destination vertex tt from from every vertex. every vertex.

Single-pair shortest-path problemSingle-pair shortest-path problem: Find a : Find a shortest path from shortest path from uu to to vv for given vertices for given vertices uu and and vv. If we solve the single-source shortest paths . If we solve the single-source shortest paths problem, we also solve this problem.problem, we also solve this problem.

All-pairs shortest-paths problemAll-pairs shortest-paths problem: Find a shortest : Find a shortest path from path from uu to to vv for every pair of vertices for every pair of vertices uu and and vv. . This can be solved by the single-source problem This can be solved by the single-source problem run for each vertex. run for each vertex.

Page 25: Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path

SSP – Dijkstra’s AlgorithmSSP – Dijkstra’s Algorithm

Working:Working:1.1. It determines the shortest path from given origin and all It determines the shortest path from given origin and all

other vertices.other vertices.

2.2. The algorithm uses a set of The algorithm uses a set of vertex setvertex set of selected vertices of selected vertices and an array weight where weight of the shortest path from and an array weight where weight of the shortest path from vertex 0 to vertex v that passes through vertices in the vertex 0 to vertex v that passes through vertices in the vertex set.vertex set.

3.3. If v is in If v is in vertex setvertex set the shortest path involves only vertices in the shortest path involves only vertices in vertex set. vertex set. However if v is not in However if v is not in vertex set vertex set then v is the then v is the only vertex along the path that is not in only vertex along the path that is not in vertex set. vertex set. That is That is the path ends with an edge from a vertex in the path ends with an edge from a vertex in vertex set vertex set to v. to v.

4.4. Initially the Initially the vertex setvertex set contains only vertex 0, and weight contains only vertex 0, and weight contains the weights of the single edge paths from vertex 0 contains the weights of the single edge paths from vertex 0 to all other vertices. That is weight[v] equals matrix[0][v] for to all other vertices. That is weight[v] equals matrix[0][v] for all v, where matrix is the adjacency matrix. Thus initially the all v, where matrix is the adjacency matrix. Thus initially the weight is the first row of the matrix.weight is the first row of the matrix.

Page 26: Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path

SSP – Dijkstra’s Algorithm Cont...SSP – Dijkstra’s Algorithm Cont...

After initialization find a vertex v that is not in After initialization find a vertex v that is not in vertex setvertex set and that minimizes weight[v]. Add v to and that minimizes weight[v]. Add v to vertex setvertex set . . For all unselected vertices u are not in For all unselected vertices u are not in vertex setvertex set check the values weight[u] to ensure check the values weight[u] to ensure that they are indeed minimum.that they are indeed minimum.

To make this determination break the path from 0 To make this determination break the path from 0 to u into two pieces and find their weights as to u into two pieces and find their weights as follows:follows:

Weight[v] = weight of the shortest path from 0 to vWeight[v] = weight of the shortest path from 0 to vMatrix[v][u] = weight of the shortest edge from v to uMatrix[v][u] = weight of the shortest edge from v to u

Compare the weight[u] with weight[v] |+ matrix[v]Compare the weight[u] with weight[v] |+ matrix[v][u] and let[u] and let

Weight [u] = the smaller of the values weight[u] and Weight [u] = the smaller of the values weight[u] and weight[v] + matrix [v] [u] weight[v] + matrix [v] [u]

Page 27: Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path

Example: Dijkstra’s AlgorithmExample: Dijkstra’s Algorithm

0

2

7

3 2 8

5

1

54

e

cb

d

a

Page 28: Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path

Example: Dijkstra’s AlgorithmExample: Dijkstra’s Algorithm

0

2

7

3 2 8

5

1

54

e

cb

d

a

Page 29: Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path

Example: Dijkstra’s AlgorithmExample: Dijkstra’s Algorithm

0

7

2

7

3 2 8

5

1

54

e

cb

d

a

Page 30: Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path

Example: Dijkstra’s AlgorithmExample: Dijkstra’s Algorithm

0

2

7

2

7

3 2 8

5

1

54

Page 31: Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path

Example: Dijkstra’s AlgorithmExample: Dijkstra’s Algorithm

0

2

7

2

7

3 2 8

5

1

54

Page 32: Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path

Example: Dijkstra’s AlgorithmExample: Dijkstra’s Algorithm

0

2

105

72

7

3 2 8

5

1

54

Page 33: Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path

Example: Dijkstra’s AlgorithmExample: Dijkstra’s Algorithm

0

2

65

72

7

3 2 8

5

1

54

Page 34: Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path

Example: Dijkstra’s AlgorithmExample: Dijkstra’s Algorithm

0

2

65

72

7

3 2 8

5

1

54

Page 35: Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path

Example: Dijkstra’s AlgorithmExample: Dijkstra’s Algorithm

0

2

65

72

7

3 2 8

5

1

54

Page 36: Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path

PseudocodePseudocode

1. n = number of vertices in the graph1. n = number of vertices in the graph2. 2. For(v=0 through n-1)For(v=0 through n-1)

weight [v] = matrix[0][v]weight [v] = matrix[0][v]3.3. For(step =2 through n)For(step =2 through n)

{ { find the smallest weight [v] such that v is not in find the smallest weight [v] such that v is not in vertexsetvertexsetAdd v to Add v to vertexsetvertexset//check the weight[u] for all u not in vertexset//check the weight[u] for all u not in vertexsetFor(all vertices u not in For(all vertices u not in vertexset vertexset )) if (weight [u] > weight [v] + matrix [v][u])if (weight [u] > weight [v] + matrix [v][u]) weight [u] = weight [v] + matrix [v][u]weight [u] = weight [v] + matrix [v][u]}}

Page 37: Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path

Graph and the SPGraph and the SPA trace of the shortest-A trace of the shortest-path algorithmpath algorithm

Checking Checking weightweight[u] by examining the graph:[u] by examining the graph:

Page 38: Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path

The Bellman-Ford AlgorithmThe Bellman-Ford Algorithm

What happen if we have negative weight What happen if we have negative weight edges ?edges ?

Not a problem if UNLESS there is a negative Not a problem if UNLESS there is a negative weight cycle. Why ?weight cycle. Why ?

a b

c

d

e

6

5-3

3

2

3

-2

4

What is the least distance from a to e? That is, what is (a,e)?Take the path <a,c,b,a,c,b,a,c,b,...,e>

Page 39: Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path

The Bellman-Ford AlgorithmThe Bellman-Ford Algorithm

Solves the single-source shortest-path Solves the single-source shortest-path problem when the weights may be negative.problem when the weights may be negative.

It indicates whether or not there is a negative It indicates whether or not there is a negative weight cycle reachable from the source weight cycle reachable from the source vertex.vertex.– If there is, it indicates no solution exists.If there is, it indicates no solution exists.– If there is not, it returns the solutionIf there is not, it returns the solution. .

It returns FALSE if the graph contains a negative weight cycle that is reachable from the source.

Page 40: Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path

Bellman-Ford AlgorithmBellman-Ford Algorithm

Bellman-Ford(G,w,s)1. Initialize-Single-Source(G, s)2. for i 1 to numVertices - 13. (u,v) E[G]4. Relax(u,v,w)5. (u,v) E[G]6. if d[v] > d[u] + w(u,v)7. return FALSE8. return TRUE

Initialize-Single-Source(G,s)1. for each vertex v V[G]2. d[v] 3. [v] NIL4. d[s] 0

Relax(u,v,w)1. if d[v] > d[u] + w(u,v)2. d[v] d[u] + w(u,v)3. [u] v

Page 41: Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path

Example: Bellman-Ford AlgorithmExample: Bellman-Ford Algorithm

0

7

68 -3

9

-2

7

z

xt

y

s-42

5

Page 42: Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path

Example: Bellman-Ford AlgorithmExample: Bellman-Ford Algorithm

0

7

6

7

68 -3

9

-2

7

z

xt

y

s-42

5

Loop order: (t,x), (t, y), (t,z), (x,t), (y,x), (y,z), (z,x), (z,s), (s,t), (s,y)

Page 43: Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path

Example: Bellman-Ford AlgorithmExample: Bellman-Ford Algorithm

0

7

46

27

68 -3

9

-2

7

z

xt

y

s-42

5

Loop order: (t,x), (t, y), (t,z), (x,t), (y,x), (y,z), (z,x), (z,s), (s,t), (s,y)

Page 44: Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path

Example: Bellman-Ford AlgorithmExample: Bellman-Ford Algorithm

0

7

42

27

68 -3

9

-2

7

z

xt

y

s-42

5

Loop order: (t,x), (t, y), (t,z), (x,t), (y,x), (y,z), (z,x), (z,s), (s,t), (s,y)

Page 45: Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path

Three more Applications of Graphs ……Three more Applications of Graphs ……

Page 46: Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path

Traveling Salesman ProblemTraveling Salesman Problem

Math version:Math version: G is a G is a complete weighted graphcomplete weighted graph

– Every vertex adjacent to every otherEvery vertex adjacent to every other Find minimal cycle through all verticesFind minimal cycle through all vertices

– A Hamiltonian cycle with minimal weightA Hamiltonian cycle with minimal weight Going through each vertex only onceGoing through each vertex only once

Intuitive version:Intuitive version: We have a map with all cities and distancesWe have a map with all cities and distances We want to visit each city once (except for origin)We want to visit each city once (except for origin) We want to find minimum distance to travelWe want to find minimum distance to travel

Page 47: Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path

Three utilities problemThree utilities problem

Imagine three houses A, B, C and three utilities X, Y, Z

If the houses and the utilities are vertices in a graph, is it possible to connect each house to each utility with edges that do not cross one another?

The answer : NO Solution for this problem is beyond our scope

Page 48: Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path

Four color problemFour color problem

Given a planar graph, can you color the vertices so that no adjacent vertices have the same color, if you have only four colors?

Answer is Yes but it is difficult to prove.

Planar graph? - where none of its edges cross

This problem was posed more than a century before, it was solved in 1970’s with the use of a computer