a mathematical view of our world

Post on 14-Feb-2016

40 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

A Mathematical View of Our World. 1 st ed. Parks, Musser, Trimpe, Maurer, and Maurer. Chapter 6. Routes and Networks. Section 6.1 Routing Problems. Goals Study graphs, vertices, and edges Study paths and circuits Study connected graphs Use Euler’s Theorem Use Fleury’s Algorithm. - PowerPoint PPT Presentation

TRANSCRIPT

A Mathematical View A Mathematical View of Our Worldof Our World

11stst ed. ed.

Parks, Musser, Trimpe, Parks, Musser, Trimpe, Maurer, and MaurerMaurer, and Maurer

Chapter 6Chapter 6

Routes and NetworksRoutes and Networks

Section 6.1Section 6.1Routing ProblemsRouting Problems

• GoalsGoals• Study graphs, vertices, and edgesStudy graphs, vertices, and edges• Study paths and circuitsStudy paths and circuits• Study connected graphsStudy connected graphs

• Use Euler’s TheoremUse Euler’s Theorem• Use Fleury’s AlgorithmUse Fleury’s Algorithm

6.1 Initial Problem6.1 Initial Problem

• Can the people of Konigsberg, pictured here, take Can the people of Konigsberg, pictured here, take a walk crossing each bridge exactly one time?a walk crossing each bridge exactly one time?

• The solution will be given at the end of the section.The solution will be given at the end of the section.

GraphsGraphs

• A A graphgraph is a collection of one or more is a collection of one or more points, called points, called verticesvertices, and the paths , and the paths connecting them, called connecting them, called edgesedges..• The singular of vertices is The singular of vertices is vertexvertex..

Graphs, cont’dGraphs, cont’d• The vertices are drawn as dots.The vertices are drawn as dots.• The edges are drawn as straight or curved The edges are drawn as straight or curved

lines.lines.• Each edge has two ends with a vertex at each Each edge has two ends with a vertex at each

end, or else it is a end, or else it is a looploop connecting a vertex to connecting a vertex to itself.itself.

Equivalent GraphsEquivalent Graphs

• Two graphs are considered the same if Two graphs are considered the same if they have the same number of vertices they have the same number of vertices connected in the same way, even if they connected in the same way, even if they look different.look different.

Example 1Example 1• The three graphs shown are equivalent.The three graphs shown are equivalent.

Vertices and EdgesVertices and Edges• Two edges may Two edges may

appear to cross, but appear to cross, but they do not actually they do not actually intersect unless intersect unless there is a vertex.there is a vertex.

• Example: Graph (a) Example: Graph (a) shows two edges shows two edges crossing where crossing where there is no vertex.there is no vertex.

Example 2Example 2• Graphs can be Graphs can be

drawn to represent drawn to represent real-life connections, real-life connections, such as roads such as roads between cities.between cities.

• Example: Draw a Example: Draw a graph to represent graph to represent the roadmap.the roadmap.

Example 2, cont’dExample 2, cont’d• Solution: The vertices represent the Solution: The vertices represent the

cities, and the edges represent the roads.cities, and the edges represent the roads.

Adjacent VerticesAdjacent Vertices

• Two vertices are called Two vertices are called adjacent adjacent verticesvertices if there is an edge connecting if there is an edge connecting them.them.

Example 3Example 3• List the pairs of adjacent vertices in the graph.List the pairs of adjacent vertices in the graph.

Example 3, cont’dExample 3, cont’d• Solution: The pairs of adjacent vertices Solution: The pairs of adjacent vertices

are:are:• A and B, A and C, A and D, A and EA and B, A and C, A and D, A and E• B and DB and D• C and EC and E• E and E, since there is a loop at EE and E, since there is a loop at E

Question:Question:List all the vertices that are adjacent to the List all the vertices that are adjacent to the vertex labeled Portland.vertex labeled Portland.

a. Salem, Boise, Olympiaa. Salem, Boise, Olympiab. Salem, Spokaneb. Salem, Spokanec. Salem, Olympia, Seattle, Spokanec. Salem, Olympia, Seattle, Spokaned. Boise d. Boise

Example 4Example 4• Draw two different graphs with the Draw two different graphs with the

following propertiesfollowing properties::• Vertices: A, B, C, and DVertices: A, B, C, and D• Adjacent vertices are:Adjacent vertices are:

A and C, B and C, B and D, C and D.A and C, B and C, B and D, C and D.

Example 4, cont’dExample 4, cont’d• Solution: Two possible graphs are shown Solution: Two possible graphs are shown

below. below.

Degree of a VertexDegree of a Vertex• The The degree of a vertexdegree of a vertex is the total is the total

number of edges at the vertex.number of edges at the vertex.• Find the degree of a vertex by counting Find the degree of a vertex by counting

the number of edges leaving or arriving the number of edges leaving or arriving at the vertex.at the vertex.

• A loop contributes 2 to the degree of a A loop contributes 2 to the degree of a vertex.vertex.

Example 5Example 5• Find the degree of Find the degree of

each vertex in the each vertex in the graph.graph.

• Solution: Solution: • A has degree 5, A has degree 5, • B has degree 3, B has degree 3, • C has degree 2, C has degree 2, • D has degree 2, D has degree 2, • E has degree 4.E has degree 4.

TheoremTheorem• If If dd is the sum of all the degrees is the sum of all the degrees

of the vertices in a graph and of the vertices in a graph and ee is is the number of edges in the graph, the number of edges in the graph, then then dd = 2 = 2ee..

• Note: This theorem is true because Note: This theorem is true because each edge adds 2 to the degree total.each edge adds 2 to the degree total.

Example 6Example 6• Verify that the Verify that the

theorem is true for theorem is true for the following the following graph. graph.

• Solution: Solution: The sum The sum of the degrees is of the degrees is 16. The number of 16. The number of edges is 8. Check edges is 8. Check that 16 = 2(8).that 16 = 2(8).

PathsPaths

• A A pathpath is a route in a graph. is a route in a graph.• Each vertex used is adjacent to the next Each vertex used is adjacent to the next

vertex used.vertex used.• Each edge in the graph is used at most Each edge in the graph is used at most

one time.one time.• A path is written as a list of vertices.A path is written as a list of vertices.

Example 7Example 7• Find three different Find three different

paths in the graph. paths in the graph. • Solution:Solution:

• One possible path is One possible path is A,B,D,A,EA,B,D,A,E..

• A second path is A second path is B,A,B,D.B,A,B,D.

• A third path is A third path is C,E,E,AC,E,E,A• There are many other There are many other

paths.paths.

CircuitsCircuits• A A circuitcircuit is a path is a path

that begins and that begins and ends at the same ends at the same vertex.vertex.

• Example: One Example: One circuit in the graph circuit in the graph is A,B,D,A.is A,B,D,A.

Euler PathsEuler Paths• A path that uses every edge of a graph exactly A path that uses every edge of a graph exactly

once is called an once is called an Euler pathEuler path..• Note: Euler is pronounced “oiler.”Note: Euler is pronounced “oiler.”• Example: The edges are numbered in the Example: The edges are numbered in the

order used.order used.

Euler CircuitsEuler Circuits• A circuit that uses every edge of a graph A circuit that uses every edge of a graph

exactly once is called an exactly once is called an Euler circuitEuler circuit..• Example: The edges are numbered in the Example: The edges are numbered in the

order used.order used.

Connected GraphsConnected Graphs• A graph is said to be A graph is said to be connectedconnected if, for if, for

every pair of vertices, there is a path that every pair of vertices, there is a path that contains them.contains them.

• If a graph is not connected, it is said to be If a graph is not connected, it is said to be disconnecteddisconnected..• An edge is called a An edge is called a bridgebridge if its removal from if its removal from

the graph would change the graph from the graph would change the graph from connected to disconnected.connected to disconnected.

Example 8Example 8• Explain whether or not the graphs are Explain whether or not the graphs are

connected.connected.a)a)

• Solution: The graph is connected because Solution: The graph is connected because there is a path, using one edge or more, there is a path, using one edge or more, between every pair of vertices.between every pair of vertices.

Example 8, cont’dExample 8, cont’db)b)

• Solution 2: The graph is disconnected Solution 2: The graph is disconnected because, for example, there is no path because, for example, there is no path between vertex A and vertex G.between vertex A and vertex G.

Example 9Example 9

• Identify any bridges in the (connected) Identify any bridges in the (connected) graph. graph.

• Solution: There are three bridges: the edge Solution: There are three bridges: the edge between D and E, the edge between D and F, between D and E, the edge between D and F, and the edge between F and G.and the edge between F and G.

ComponentsComponents• A A componentcomponent of a graph is a maximal of a graph is a maximal

connected piece of the graph.connected piece of the graph.• A component is connected.A component is connected.• If any more edges are added to the If any more edges are added to the

component (if there are any more in the component (if there are any more in the graph) it would become disconnected.graph) it would become disconnected.

• Every graph has one or more Every graph has one or more components.components.

Components, cont’dComponents, cont’d• Any connected graph has one component.Any connected graph has one component.

• For the connected graph in the previous For the connected graph in the previous example, the entire graph is the only example, the entire graph is the only component.component.

• A disconnected graph has two or more A disconnected graph has two or more components.components.• For the disconnected graph in the previous For the disconnected graph in the previous

example, there are two components.example, there are two components.

Components, cont’dComponents, cont’d• To find the components of a graph, use To find the components of a graph, use

the following algorithm.the following algorithm.1) Pick any vertex of the graph and highlight it.

2) Highlight all edges connecting to the highlighted vertex and all vertices at the end of those edges.

3) Repeat step 2 as many times as possible.

4) When no more vertices can be highlighted, you have a component.

Example 10Example 10• Find all components of the graph.Find all components of the graph.

• Solution: We use the algorithm from the Solution: We use the algorithm from the preceding slide. There are three components: preceding slide. There are three components: the piece containing A, E, F, and G; the piece the piece containing A, E, F, and G; the piece containing B and C; and the piece containing D.containing B and C; and the piece containing D.

Degree TheoremDegree Theorem• Any graph must have an even number of Any graph must have an even number of

vertices with odd degree.vertices with odd degree.• We saw earlier that the sum of all the We saw earlier that the sum of all the

degrees in a graph is always an even degrees in a graph is always an even number, so:number, so:

• Vertices with even degrees do not have to Vertices with even degrees do not have to come in pairs.come in pairs.

• Vertices with odd degrees come in pairs.Vertices with odd degrees come in pairs.

Euler’s TheoremEuler’s Theorem

• For a connected graph:For a connected graph:• If the graph has zero vertices of odd If the graph has zero vertices of odd

degree, then it has at least one degree, then it has at least one Euler circuit. Euler circuit.

• If a graph has an Euler circuit, then If a graph has an Euler circuit, then it has no vertices of odd degree.it has no vertices of odd degree.

Euler’s Theorem cont’dEuler’s Theorem cont’d• For a connected graph:For a connected graph:

• If the graph has exactly two vertices of If the graph has exactly two vertices of odd degree, thenodd degree, then

• It does not have an Euler circuit.It does not have an Euler circuit.• It has at least one Euler path.It has at least one Euler path.• Any Euler path starts at one of the two Any Euler path starts at one of the two

vertices of odd degree and ends at the vertices of odd degree and ends at the other.other.

Euler’s Theorem cont’dEuler’s Theorem cont’d

• For a connected graph:For a connected graph:• If the graph has more than two If the graph has more than two

vertices of odd degree, thenvertices of odd degree, then• It does not have an Euler circuit.It does not have an Euler circuit.• It does not have an Euler path.It does not have an Euler path.

Question:Question:

Does the graph have an Euler circuit, an Does the graph have an Euler circuit, an Euler path, or neither? Euler path, or neither?

a. Euler circuita. Euler circuitb. Euler path onlyb. Euler path onlyc. neitherc. neither

Example 11Example 11

a)a) Solution: Every Solution: Every vertex is of degree vertex is of degree 2 or degree 4, so 2 or degree 4, so by Euler’s by Euler’s theorem the graph theorem the graph has an Euler has an Euler circuit.circuit.

• Does the graph have an Euler circuit, Does the graph have an Euler circuit, Euler path, or neither?Euler path, or neither?

Example 11, cont’dExample 11, cont’d

b)b) Solution: There are two vertices of odd Solution: There are two vertices of odd degree, E and G, so by Euler’s theorem degree, E and G, so by Euler’s theorem the graph has only an Euler path.the graph has only an Euler path.

Fleury’s AlgorithmFleury’s Algorithm• If Euler’s Theorem indicates that there is If Euler’s Theorem indicates that there is

an Euler circuit, Fleury’s Algorithm can an Euler circuit, Fleury’s Algorithm can be used to find the circuit.be used to find the circuit.

• The step-by-step procedure is as follows:The step-by-step procedure is as follows:1)1) Copy all the vertices, but not the edges, of Copy all the vertices, but not the edges, of

the original graph.the original graph.

2)2) Select any vertex on the original graph and Select any vertex on the original graph and mark it as the present position.mark it as the present position.

Fleury’s Algorithm, cont’dFleury’s Algorithm, cont’d

3)3) Choose one edge at the marked Choose one edge at the marked vertex and transfer it to the new vertex and transfer it to the new graph.graph.

• Do not choose a bridge, unless it is Do not choose a bridge, unless it is the only edge available.the only edge available.

• Number the edges in the order they Number the edges in the order they are transferred.are transferred.

Fleury’s Algorithm, cont’dFleury’s Algorithm, cont’d

4)4) Use the vertex on the other end of Use the vertex on the other end of the edge as the new present the edge as the new present position. Repeat step 3.position. Repeat step 3.

5)5) When you transfer the last edge When you transfer the last edge you are done.you are done.

Example 12Example 12• Use Fleury’s Algorithm to find an Euler circuit Use Fleury’s Algorithm to find an Euler circuit

for the graph.for the graph.

• Possible Final Solution: Possible Final Solution:

6.1 Initial Problem Solution6.1 Initial Problem Solution• The idea of Euler The idea of Euler

circuits can be used circuits can be used to solve the to solve the question of whether question of whether the people of the people of Konigsberg can take Konigsberg can take a walk crossing a walk crossing each bridge exactly each bridge exactly one time.one time.

Initial Problem Solution, cont’dInitial Problem Solution, cont’d• The vertices represent The vertices represent

the land and the edges the land and the edges the bridges. The the bridges. The described walk would described walk would be an Euler path.be an Euler path.

• We use Euler’s We use Euler’s Theorem to show that Theorem to show that there is no such path there is no such path for their walk because for their walk because there are four vertices there are four vertices of odd degree.of odd degree.

Section 6.2Section 6.2Network ProblemsNetwork Problems

• GoalsGoals• Study weighted graphsStudy weighted graphs• Study spanning treesStudy spanning trees

• Study minimal spanning treesStudy minimal spanning trees• Use Kruskal’s algorithmUse Kruskal’s algorithm

6.2 Initial Problem6.2 Initial Problem

• Walkways need Walkways need to be built to be built between the between the buildings on the buildings on the campus pictured campus pictured at right. at right.

6.2 Initial Problem, cont’d6.2 Initial Problem, cont’d• How can the walkways be How can the walkways be

constructed so that:constructed so that:• Students can go from any building on Students can go from any building on

campus to any other building without campus to any other building without needing to walk outside.needing to walk outside.

• The total length of the walkways is The total length of the walkways is minimized.minimized.

• The solution will be given at the end of the The solution will be given at the end of the sectionsection..

Weighted GraphsWeighted Graphs

• A graph in which each edge has a A graph in which each edge has a number associated with it is called a number associated with it is called a weighted graphweighted graph..

• The number corresponding to an edge is The number corresponding to an edge is called the called the weightweight of the edge. of the edge.

Weighted Graphs, cont’dWeighted Graphs, cont’d• Typically the weights of the edges in a Typically the weights of the edges in a

graph are assigned in a way that graph are assigned in a way that represents some physical situation or represents some physical situation or practical problem.practical problem.

• The lengths of the edges in a graph The lengths of the edges in a graph do not have to be proportional to the do not have to be proportional to the stated weights. stated weights.

Weighted Graphs, cont’dWeighted Graphs, cont’d• An example of a weighted graph is shown below. An example of a weighted graph is shown below.

Example 1Example 1• Suppose Ed has:Suppose Ed has:

• A 35-minute drive from his home to his A 35-minute drive from his home to his workplaceworkplace

• A 15-minute drive from his workplace to his A 15-minute drive from his workplace to his health clubhealth club

• A 25 minute drive from his health club to his A 25 minute drive from his health club to his homehome

• Draw a weighted graph to represent this Draw a weighted graph to represent this situation.situation.

Example 1, cont’dExample 1, cont’d• Solution:Solution:

• The vertices of the graph will represent Ed’s The vertices of the graph will represent Ed’s home (home (HH), his workplace (), his workplace (WW), and his health ), and his health club (club (CC).).

• The weight of each edge will represent the The weight of each edge will represent the driving time between the two places.driving time between the two places.

• For simplicity, we can draw all the edges as For simplicity, we can draw all the edges as straight lines and with the same length. straight lines and with the same length.

Example 1, cont’dExample 1, cont’d• Solution, cont’d: One possible weighted graph Solution, cont’d: One possible weighted graph

is shown below.is shown below.

Redundant EdgesRedundant Edges• Redundant edgesRedundant edges are edges that can are edges that can

be removed while still leaving a path be removed while still leaving a path between the two vertices.between the two vertices.

• Example: If edge Example: If edge CHCH is removed is removed from the previous graph, Ed can still from the previous graph, Ed can still get from home to his club by driving get from home to his club by driving to work and then to the club. to work and then to the club.

• The edge The edge CHCH is a redundant edge. is a redundant edge.

Question:Question:

Are there any redundant edges in the Are there any redundant edges in the graph? graph?

a. yesa. yesb. nob. no

SubgraphsSubgraphs• A A subgraphsubgraph of a of a

given graph is a set given graph is a set of vertices and of vertices and edges chosen from edges chosen from among those of the among those of the original graph.original graph.

• Example: Removing Example: Removing edge edge CHCH creates creates the subgraph shown the subgraph shown at right.at right.

Subgraphs, cont’dSubgraphs, cont’d• Example: Example:

Another Another subgraph could subgraph could be created by be created by removing removing vertex vertex CC and its and its two incident two incident edges. edges.

Example 2Example 2• A bus line goes between the cities shown in A bus line goes between the cities shown in

the table.the table.a)a) Draw a weighted graph to represent the Draw a weighted graph to represent the

situation.situation.

b)b) Find two different routes from Selma to Anniston.Find two different routes from Selma to Anniston.

Example 2, cont’dExample 2, cont’da)a) Solution: A weighted graph is shown below.Solution: A weighted graph is shown below.

Example 2, cont’dExample 2, cont’d

b)b) Solution: One Solution: One possible route possible route from Selma to from Selma to Anniston is Anniston is SMGASMGA, with a total , with a total weight (length) of weight (length) of 49 + 127 + 27 = 49 + 127 + 27 = 203 miles.203 miles.

Example 2, cont’dExample 2, cont’db)b) Solution, cont’d: Solution, cont’d:

Another possible Another possible route from Selma route from Selma to Anniston is to Anniston is SBASBA, with a total , with a total weight (length) of weight (length) of 160 + 62 = 222 160 + 62 = 222 miles.miles.

TreesTrees• One way to recognize that a graph One way to recognize that a graph

contains redundant edges is to find a contains redundant edges is to find a circuit in the graph.circuit in the graph.

• Recall that a circuit is a path that begins Recall that a circuit is a path that begins at a vertex and returns to that vertex at a vertex and returns to that vertex without using any edges twice.without using any edges twice.

• A connected graph that has no A connected graph that has no circuits is called a circuits is called a treetree. .

Question:Question:

Choose the graph that is a tree. Choose the graph that is a tree.

a.a. b. b.

c.c. d.d.

Trees, cont’dTrees, cont’d• The following figure gives examples of graphs The following figure gives examples of graphs

that are trees and a graph that is not a tree. that are trees and a graph that is not a tree.

Example 3Example 3• This graph is not a tree because it This graph is not a tree because it

contains several circuits. contains several circuits. • Find a subgraph that is a tree.Find a subgraph that is a tree.

Example 3, cont’dExample 3, cont’d• Solution: Circuits are highlighted and Solution: Circuits are highlighted and

edges are removed until the remaining edges are removed until the remaining subgraph has no circuits left. subgraph has no circuits left.

Spanning TreesSpanning Trees

• A A spanning treespanning tree is a subgraph that: is a subgraph that: • Contains all the original vertices of the Contains all the original vertices of the

graph.graph.• Is connected.Is connected.• Contains no circuits.Contains no circuits.

Spanning Trees, cont’dSpanning Trees, cont’d• Every connected graph has at least one Every connected graph has at least one

spanning tree.spanning tree.• An example of a spanning tree is shown An example of a spanning tree is shown

below.below.

Minimal Spanning TreesMinimal Spanning Trees• A spanning tree with the smallest A spanning tree with the smallest

possible total weight is called a possible total weight is called a minimal spanning treeminimal spanning tree..

• Since a connected, weighted graph can Since a connected, weighted graph can have multiple spanning trees, there may have multiple spanning trees, there may be one or more than one of those be one or more than one of those spanning trees with the smallest total spanning trees with the smallest total weight.weight.

Minimal Spanning Trees, cont’dMinimal Spanning Trees, cont’d• For the connected, weighted graph shown For the connected, weighted graph shown

below:below:• Subgraph (b) is not a minimal spanning tree.Subgraph (b) is not a minimal spanning tree.• Subgraph (c) is a minimal spanning tree. Subgraph (c) is a minimal spanning tree.

Kruskal’s AlgorithmKruskal’s Algorithm• A method known as Kruskal’s A method known as Kruskal’s

algorithm can be used to find a algorithm can be used to find a minimal spanning tree of a minimal spanning tree of a connected, weighted graph.connected, weighted graph.

• Kruskal’s algorithm begins with all Kruskal’s algorithm begins with all the vertices of a graph and adds the vertices of a graph and adds edges one by one, using the idea of edges one by one, using the idea of acceptable and unacceptable edges.acceptable and unacceptable edges.

Kruskal’s Algorithm, cont’dKruskal’s Algorithm, cont’d• Acceptable edgesAcceptable edges are are

• Edges that do not share a vertex with Edges that do not share a vertex with any edges already chosen.any edges already chosen.

Kruskal’s Algorithm, cont’dKruskal’s Algorithm, cont’d• Acceptable edgesAcceptable edges are are

• Edges that connect two components of Edges that connect two components of the subgraph. the subgraph.

Kruskal’s Algorithm, cont’dKruskal’s Algorithm, cont’d• Acceptable edgesAcceptable edges are are

• Edges that connect a component of the Edges that connect a component of the subgraph to a new vertex. subgraph to a new vertex.

Kruskal’s Algorithm, cont’dKruskal’s Algorithm, cont’d• Unacceptable edgesUnacceptable edges are are

• Edges that add to a component of the Edges that add to a component of the subgraph, but do not add a new vertex. subgraph, but do not add a new vertex.

Kruskal’s Algorithm, cont’dKruskal’s Algorithm, cont’d1)1) Consider only the vertices of the graph.Consider only the vertices of the graph.2)2) Add the edge with the smallest weight.Add the edge with the smallest weight.3)3) Add the acceptable edge with the smallest Add the acceptable edge with the smallest

weight.weight.4)4) Determine whether all vertices are Determine whether all vertices are

connected by a path.connected by a path.a)a) If so, you have a minimal spanning tree.If so, you have a minimal spanning tree.

b)b) If not, repeat Step 3. If not, repeat Step 3.

Example 4Example 4

• Construct a Construct a minimal minimal spanning tree for spanning tree for the weighted the weighted graph.graph.

Example 4, cont’dExample 4, cont’d

• Solution:Solution:• Start with the 4 vertices.Start with the 4 vertices.• Add the edge with the smallest weight.Add the edge with the smallest weight.

Example 4, cont’dExample 4, cont’d• Solution, cont’d:Solution, cont’d:

• All unused edges All unused edges are acceptable, are acceptable, so we simply so we simply choose the edge choose the edge with the smallest with the smallest weight.weight.

• Select the edge Select the edge with weight 2.6.with weight 2.6.

Example 4, cont’dExample 4, cont’d• Solution, cont’d:Solution, cont’d:

• The acceptable The acceptable edges at this edges at this step were the step were the ones with ones with weights 4.8 and weights 4.8 and 3.7.3.7.

• Select the edge Select the edge with weight 3.7.with weight 3.7.

Example 4, cont’dExample 4, cont’d• Solution, cont’d:Solution, cont’d:

• The subgraph is The subgraph is now connected, now connected, so this is a so this is a minimal minimal spanning tree.spanning tree.

• The weight of The weight of the tree is 1.5 + the tree is 1.5 + 2.6 + 3.7 = 7.8.2.6 + 3.7 = 7.8.

6.2 Initial Problem Solution6.2 Initial Problem Solution• How can a college How can a college

build walkways of build walkways of the minimum length the minimum length possible so that possible so that students can go students can go from any building from any building on campus to any on campus to any other building other building without needing to without needing to walk outside?walk outside?

Initial Problem Solution, cont’dInitial Problem Solution, cont’d• Create a weighted Create a weighted

graph to represent graph to represent the buildings and the buildings and existing sidewalks existing sidewalks on the campus.on the campus.

• The weight of each The weight of each edge is the distance edge is the distance between the two between the two buildings.buildings.

Initial Problem Solution, cont’dInitial Problem Solution, cont’d

• Use Kruskal’s Use Kruskal’s algorithm to find a algorithm to find a minimal spanning minimal spanning tree.tree.

• The edges included in The edges included in the minimal spanning the minimal spanning tree represent the tree represent the sidewalks that should sidewalks that should be converted into be converted into covered walkways.covered walkways.

Initial Problem Solution, cont’dInitial Problem Solution, cont’d• Under this plan, students Under this plan, students

can get from any building can get from any building to any other building to any other building without going outside, without going outside, although they may have although they may have to walk farther inside.to walk farther inside.

• The length of the The length of the walkways was minimized walkways was minimized to a total of 210 + 220 + to a total of 210 + 220 + 120 + 200 = 750 feet. 120 + 200 = 750 feet.

Section 6.3Section 6.3Traveling-Salesperson ProblemTraveling-Salesperson Problem

• GoalsGoals• Study Hamiltonian circuitsStudy Hamiltonian circuits• Study the Traveling-Salesperson Study the Traveling-Salesperson

ProblemProblem• Use the Brute-Force AlgorithmUse the Brute-Force Algorithm• Use the Nearest-Neighbor AlgorithmUse the Nearest-Neighbor Algorithm• Use the Cheapest-Link AlgorithmUse the Cheapest-Link Algorithm

6.3 Initial Problem6.3 Initial Problem

• An author needs to find a route that will minimize An author needs to find a route that will minimize the total distance she must drive between the the total distance she must drive between the cities.cities.

• The solution will be given at the end of the section.The solution will be given at the end of the section.

Hamiltonian CircuitsHamiltonian Circuits

• A path that visits each vertex in a A path that visits each vertex in a graph exactly once is called a graph exactly once is called a Hamiltonian pathHamiltonian path..

• If a Hamiltonian path begins and ends If a Hamiltonian path begins and ends at the same vertex, it is called a at the same vertex, it is called a Hamiltonian circuitHamiltonian circuit..

Example 1Example 1

• The highlighted The highlighted edges in the graph edges in the graph represent a represent a Hamiltonian path. Hamiltonian path.

• The path can be The path can be written written AGFEDBCAGFEDBC or or CBDEFGA.CBDEFGA.

Example 2Example 2

• The highlighted The highlighted edges in the graph edges in the graph represent a represent a Hamiltonian circuit.Hamiltonian circuit.

• The circuit, starting at The circuit, starting at AA, can be written , can be written AGFEDCBAAGFEDCBA

Hamiltonian Circuits, cont’dHamiltonian Circuits, cont’d

• Note: A Hamiltonian path or circuit must Note: A Hamiltonian path or circuit must pass through every vertex of the graph, pass through every vertex of the graph, but it does not have to use every edge.but it does not have to use every edge.

• A Hamiltonian path can be used, for A Hamiltonian path can be used, for example, to model a delivery truck example, to model a delivery truck stopping at every house in a stopping at every house in a neighborhood but not using every street.neighborhood but not using every street.

Example 3Example 3

• Find a Hamiltonian path in each graph Find a Hamiltonian path in each graph or explain why you cannot.or explain why you cannot.

Example 3, cont’dExample 3, cont’da)a) Solution: The path Solution: The path ABCDABCD is a Hamiltonian path. is a Hamiltonian path.

• Adding an edge to the path forms the Hamiltonian circuit Adding an edge to the path forms the Hamiltonian circuit ABCDAABCDA..

• Other Hamiltonian paths and circuits are possible.Other Hamiltonian paths and circuits are possible.

Example 3, cont’dExample 3, cont’db)b) Solution: The path Solution: The path ABCDABCD is a Hamiltonian is a Hamiltonian

path.path.• There is no Hamiltonian circuit.There is no Hamiltonian circuit.

Example 3, cont’dExample 3, cont’dc)c) There is no Hamiltonian path.There is no Hamiltonian path.

• Any Hamiltonian path must start at either Any Hamiltonian path must start at either AA or or FF..• Any path that starts at Any path that starts at AA will leave out either will leave out either CC, , DD, or , or

F.F.• Any path that starts at Any path that starts at FF will leave out either will leave out either CC, , DD, or , or

A.A.

Complete GraphsComplete Graphs

• A A complete graphcomplete graph is a graph in which is a graph in which everyevery pair of vertices is connected by pair of vertices is connected by exactly oneexactly one edge. edge.

Example 4Example 4

• For each graph, determine whether or not For each graph, determine whether or not it is complete.it is complete.

Example 4, cont’dExample 4, cont’d

a)a) Solution: Check each pair of vertices. Solution: Check each pair of vertices. • There is exactly one edge between each pair There is exactly one edge between each pair

of vertices, so the graph is complete.of vertices, so the graph is complete.

Example 4, cont’dExample 4, cont’d

b)b) Solution: Check each pair of vertices. Solution: Check each pair of vertices. • There is no edge from There is no edge from AA to to CC, for example. , for example.

The graph is not complete.The graph is not complete.

Example 4, cont’dExample 4, cont’d

c)c) Solution: Check each pair of vertices. Solution: Check each pair of vertices. • There are two edges from There are two edges from AA to to FF, for , for

example. The graph is not complete.example. The graph is not complete.

Example 5Example 5

• Draw an example of a complete graph Draw an example of a complete graph with five vertices.with five vertices.

• Count the number of edges in the Count the number of edges in the graphgraph. .

Example 5, cont’dExample 5, cont’d• Solution: Two possible representations of Solution: Two possible representations of

such a graph are shown below.such a graph are shown below.• A complete graph with 5 vertices has 10 A complete graph with 5 vertices has 10

edges.edges.

Theorem on Complete GraphsTheorem on Complete Graphs

• The number of edges in a complete The number of edges in a complete graph always follows a certain graph always follows a certain pattern.pattern.

• A complete graph with A complete graph with nn vertices vertices

always has always has edges. edges. 12

n n

Question:Question:

A graph has 20 vertices and 195 A graph has 20 vertices and 195 edges. Is the graph complete? edges. Is the graph complete?

a. yesa. yesb. no b. no

Complete Graphs, cont’dComplete Graphs, cont’d• In general, a graph may or may not have a In general, a graph may or may not have a

Hamiltonian path or circuit.Hamiltonian path or circuit.• A complete graph, however, always has a A complete graph, however, always has a

Hamiltonian circuit.Hamiltonian circuit.• Any list of all the vertices in the graph will be a Any list of all the vertices in the graph will be a

Hamiltonian path.Hamiltonian path.• Adding the starting vertex to the end of the list Adding the starting vertex to the end of the list

describing the path will create a Hamiltonian describing the path will create a Hamiltonian circuit. circuit.

Example 6Example 6

• List all the List all the Hamiltonian Hamiltonian paths and all the paths and all the Hamiltonian Hamiltonian circuits in the circuits in the graph.graph.

Example 6, cont’dExample 6, cont’d• Solution:Solution:

Example 6, cont’dExample 6, cont’d• Note that the complete graph in the Note that the complete graph in the

previous example had 3 vertices and 6 previous example had 3 vertices and 6 different Hamiltonian circuits.different Hamiltonian circuits.

• This is the case because each circuit can This is the case because each circuit can begin at one of the 3 vertices, travel to one begin at one of the 3 vertices, travel to one of 2 vertices second, and travel to the 1 of 2 vertices second, and travel to the 1 remaining vertex last.remaining vertex last.

• There are 3(2)(1) = 6 Hamiltonian circuits.There are 3(2)(1) = 6 Hamiltonian circuits.

Theorem on Complete GraphsTheorem on Complete Graphs

• The number of Hamiltonian paths in a The number of Hamiltonian paths in a complete graph with complete graph with nn vertices is vertices is

nn! = ! = nn((nn – 1)( – 1)(nn – 2)( – 2)(nn – 3)…(1). – 3)…(1).• The symbol The symbol nn! is read “n factorial”.! is read “n factorial”.

Question:Question:

How many possible Hamiltonian How many possible Hamiltonian circuits exist in a complete graph circuits exist in a complete graph with 9 vertices? with 9 vertices?

a. 81a. 81b. 40,320b. 40,320c. 362,880c. 362,880d. 3,265,920d. 3,265,920

Traveling-Salesperson ProblemTraveling-Salesperson Problem• A salesperson who must visit various A salesperson who must visit various

cities and then return home wants to cities and then return home wants to minimize the cost (measured in distance, minimize the cost (measured in distance, time, or money) of his or her trip.time, or money) of his or her trip.

• Because Hamiltonian circuits can be Because Hamiltonian circuits can be applied to find this type of least-cost trip, applied to find this type of least-cost trip, the problem of finding a least-cost the problem of finding a least-cost Hamiltonian circuit is called the Hamiltonian circuit is called the Traveling-Salesperson ProblemTraveling-Salesperson Problem..

Complete Weighted GraphsComplete Weighted Graphs

• The The costcost of a path in a weighted of a path in a weighted graph is the sum of the weights graph is the sum of the weights assigned to the edges in the path.assigned to the edges in the path.

• When costs are assigned to each When costs are assigned to each edge in a complete graph, the graph edge in a complete graph, the graph is called a is called a complete weighted graphcomplete weighted graph. .

Question:Question:

What is the cost of the path What is the cost of the path ABDABD in in the graph below? the graph below?

a. 26a. 26b. 14b. 14c. 24c. 24d. 41d. 41

Example 7Example 7

• List all possible List all possible Hamiltonian paths, Hamiltonian paths, and their costs, in and their costs, in the complete the complete weighted graph.weighted graph.

• Find the path of Find the path of least cost. least cost.

Example 7, cont’dExample 7, cont’d• Solution: The graph Solution: The graph

has 4 vertices, so has 4 vertices, so there are 4! = 24 there are 4! = 24 different Hamiltonian different Hamiltonian paths.paths.

• These paths and their These paths and their costs are listed on the costs are listed on the next slide. next slide.

Example 7, cont’dExample 7, cont’d

Example 7, cont’dExample 7, cont’d

• Solution, cont’d: There are 2 Hamiltonian paths Solution, cont’d: There are 2 Hamiltonian paths with the lowest cost of 37.with the lowest cost of 37.

• CDABCDAB• BADCBADC

Example 7, cont’dExample 7, cont’d• List all possible List all possible

Hamiltonian Hamiltonian circuits that start circuits that start and end at vertex and end at vertex A.A.

• Find the circuit at Find the circuit at AA of least cost. of least cost.

Example 7, cont’dExample 7, cont’d• Solution:Solution:

Example 7, cont’dExample 7, cont’d• Solution, cont’d: Solution, cont’d:

There are 2 least-There are 2 least-cost Hamiltonian cost Hamiltonian circuits with a cost of circuits with a cost of 74.74.

• ABCDEAABCDEA• AEDCBAAEDCBA

• These 2 circuits are These 2 circuits are mirror imagesmirror images of each of each other.other.

Traveling-Salesperson, cont’dTraveling-Salesperson, cont’d

• The method used to solve the previous The method used to solve the previous example is called the example is called the Brute-Force Brute-Force AlgorithmAlgorithm. .

• List all possible Hamiltonian circuits.List all possible Hamiltonian circuits.• Calculate the cost of each circuit.Calculate the cost of each circuit.• Find the 2 mirror image circuits of least cost.Find the 2 mirror image circuits of least cost.

• This method is very time-consuming.This method is very time-consuming.

Traveling-Salesperson, cont’dTraveling-Salesperson, cont’d

• Instead of the Brute-Force Algorithm, Instead of the Brute-Force Algorithm, faster approximation algorithms are faster approximation algorithms are typically used.typically used.

• An An approximation algorithmapproximation algorithm finds a finds a Hamiltonian circuit that is either the least-Hamiltonian circuit that is either the least-cost circuit or is one that is not much cost circuit or is one that is not much more costly than the least-cost circuit.more costly than the least-cost circuit.

Traveling-Salesperson, cont’dTraveling-Salesperson, cont’d

• Two approximation algorithms will Two approximation algorithms will be studied:be studied:

• The Nearest-Neighbor AlgorithmThe Nearest-Neighbor Algorithm• The Cheapest-Link AlgorithmThe Cheapest-Link Algorithm

Nearest-Neighbor AlgorithmNearest-Neighbor Algorithm1)1) Specify a starting vertex.Specify a starting vertex.2)2) If unvisited vertices remain, go from If unvisited vertices remain, go from

the current vertex to the unused the current vertex to the unused vertex with the lowest-cost vertex with the lowest-cost connecting edge.connecting edge.

3)3) If no unvisited vertices remain, If no unvisited vertices remain, return to the starting vertex to finish return to the starting vertex to finish forming the circuit. forming the circuit.

Example 8Example 8

• Apply the algorithm Apply the algorithm to approximate the to approximate the lowest-cost lowest-cost Hamiltonian circuit. Hamiltonian circuit.

Example 8, cont’dExample 8, cont’d• Solution:Solution:

• Let the starting vertex be Let the starting vertex be AA..• The unvisited vertices are The unvisited vertices are B, C, DB, C, D, and , and

EE. . • The choice of edges are The choice of edges are ABAB: 13, : 13, ACAC: 11, : 11,

ADAD: 17, or : 17, or AEAE: 15. : 15. • Choose edge Choose edge ACAC and move to vertex and move to vertex CC, the , the

new current vertex.new current vertex.

Example 8, cont’dExample 8, cont’d• Solution, cont’d:Solution, cont’d:

• The unvisited The unvisited vertices are vertices are B, B, DD, and , and EE. .

• The choice of The choice of edges are edges are CBCB: : 20, 20, CDCD: 12, or : 12, or CECE: 14. : 14.

• Choose edge Choose edge CD.CD.

Example 8, cont’dExample 8, cont’d• Solution, cont’d:Solution, cont’d:

• The current vertex The current vertex is now is now DD..

• The unvisited The unvisited vertices are vertices are B B and and EE. .

• The choice of The choice of edges are edges are DBDB: 19 : 19 or or DEDE: 16. : 16.

• Choose edge Choose edge DE.DE.

Example 8, cont’dExample 8, cont’d

• Solution, cont’d:Solution, cont’d:• The current The current

vertex is now vertex is now EE..• The unvisited The unvisited

vertex is vertex is BB. . • Choose edge Choose edge

EB.EB.

Example 8, cont’dExample 8, cont’d• Solution, cont’d:Solution, cont’d:

• The current The current vertex is now vertex is now BB..

• There are no There are no unvisited unvisited vertices. vertices.

• Return to vertex Return to vertex AA using edge using edge BA.BA.

Example 8, cont’dExample 8, cont’d• Solution, cont’d:Solution, cont’d:

• The Hamiltonian The Hamiltonian circuit is circuit is ACDEBAACDEBA..

• Its cost is 70. Its cost is 70. • This is at or near This is at or near

the lowest the lowest possible cost.possible cost.

Cheapest-Link AlgorithmCheapest-Link Algorithm

1)1) All edges are acceptable and no All edges are acceptable and no edges have been selected.edges have been selected.

2)2) Choose an acceptable edge of Choose an acceptable edge of smallest weight.smallest weight.

Cheapest-Link Algorithm, cont’dCheapest-Link Algorithm, cont’d3)3) If a Hamiltonian circuit is not formed, If a Hamiltonian circuit is not formed,

determine the set of acceptable edges determine the set of acceptable edges and repeat Step 2.and repeat Step 2.

• Unacceptable edges are those that either Unacceptable edges are those that either share a vertex with two selected edges or share a vertex with two selected edges or that will close a circuit that is not that will close a circuit that is not Hamiltonian.Hamiltonian.

4)4) If the selected edges form a Hamiltonian If the selected edges form a Hamiltonian circuit, this is the low-cost Hamiltonian circuit, this is the low-cost Hamiltonian circuit. circuit.

Example 9Example 9

• Apply the algorithm Apply the algorithm to approximate the to approximate the lowest-cost lowest-cost Hamiltonian circuit. Hamiltonian circuit.

Example 9, cont’dExample 9, cont’d• Solution:Solution:

• All edges are All edges are acceptable.acceptable.

• Select the edge Select the edge of lowest cost: of lowest cost: ACAC..

Example 9, cont’dExample 9, cont’d• Solution, cont’d:Solution, cont’d:

• All edges are All edges are still acceptable.still acceptable.

• Select the edge Select the edge of lowest cost: of lowest cost: CDCD..

Example 9, cont’dExample 9, cont’d• Solution, cont’d:Solution, cont’d:

• Edges Edges BC,BC, CE, CE, and and ADAD are are unacceptable.unacceptable.

• Select the Select the acceptable edge acceptable edge of lowest cost: of lowest cost: ABAB..

Example 9, cont’dExample 9, cont’d• Solution, cont’d:Solution, cont’d:

• Edges Edges BC,BC, CE, CE, BD, BD, and and ADAD are are unacceptable.unacceptable.

• Select the Select the acceptable edge acceptable edge of lowest cost: of lowest cost: DEDE..

Example 9, cont’dExample 9, cont’d• Solution, cont’d:Solution, cont’d:

• The only The only remaining remaining acceptable edge is acceptable edge is BEBE..

• The low-cost The low-cost circuit is complete, circuit is complete, with a weight of with a weight of 70.70.

6.3 Initial Problem Solution6.3 Initial Problem Solution• An author needs to find a route that will An author needs to find a route that will

minimize the total distance she must drive minimize the total distance she must drive between the cities shown below. between the cities shown below.

Initial Problem Solution, cont’dInitial Problem Solution, cont’d

• Imagine a graph in which the vertices Imagine a graph in which the vertices represent the cities and the weighted represent the cities and the weighted edges represent the distances.edges represent the distances.

• This is a Traveling-Salesperson problem, This is a Traveling-Salesperson problem, and there are 9! = 362,880 possible and there are 9! = 362,880 possible Hamiltonian circuits.Hamiltonian circuits.

• Of these, 8! = 40,320 circuits start at New Of these, 8! = 40,320 circuits start at New Orleans.Orleans.

Initial Problem Solution, cont’dInitial Problem Solution, cont’d

• With such a large number of circuits, it is With such a large number of circuits, it is foolish to try to solve this by hand using foolish to try to solve this by hand using the Brute-Force Algorithm.the Brute-Force Algorithm.

• The Cheapest-Link Algorithm will be used The Cheapest-Link Algorithm will be used to find a low-cost Hamiltonian circuit to find a low-cost Hamiltonian circuit representing a low-cost trip for the author.representing a low-cost trip for the author.

• Rather than drawing the large graph, view the Rather than drawing the large graph, view the costs (distances) in the table. costs (distances) in the table.

Initial Problem Solution, cont’dInitial Problem Solution, cont’d• Using the algorithm, add acceptable edges Using the algorithm, add acceptable edges

one at a time.one at a time.• The first 7 edges added are shown below. The first 7 edges added are shown below.

Initial Problem Solution, cont’dInitial Problem Solution, cont’d• The cheapest acceptable edge at this point is The cheapest acceptable edge at this point is

the route between Charlotte and Orlando.the route between Charlotte and Orlando.• After that, the route from New Orleans to After that, the route from New Orleans to

Orlando must be added to complete the circuit. Orlando must be added to complete the circuit.

Initial Problem Solution, cont’dInitial Problem Solution, cont’d• The approximate least-cost Hamiltonian circuit is The approximate least-cost Hamiltonian circuit is

shown below.shown below.• The total cost of the circuit is 2462, so the author The total cost of the circuit is 2462, so the author

will drive 2462 miles if she takes this route on will drive 2462 miles if she takes this route on her trip. her trip.

top related