MAT 110 Workshop - MWSU Intranet · 2018-12-06 · • TS - 12 would make S degree 3 • TU - 13...
Transcript of MAT 110 Workshop - MWSU Intranet · 2018-12-06 · • TS - 12 would make S degree 3 • TU - 13...
MAT 110 WORKSHOPUpdated Fall 2018
UNIT 1: GRAPH
THEORYIntroduction
Terms and Definitions
• Graph: A collection of points, called vertices, together with a set of lines,
called edges, connecting pairs of vertices.
• Degree: The number of edges that use that vertex as an endpoint (loops
count twice, once for each endpoint)
• Simple Graph: One without loops or double edges
• Non-Simple Graph: One that contains at least one loop and/or double edge
• Path: A sequence of distinct edges; an ordered list of vertices
• Circuit: A path that begins and ends with the same vertex
• Length: The number of edges traveled in a given path or circuit
• Connected Graph: A graph with at least one path between every pair of
vertices
• Subgraph: A set of vertices and edges chosen from the original graph
• Component: A maximally connected subgraph
• Bridge: An edge in a connected graph that, if removed, wthe graph would no
longer be connected
Graph Terminology
Graph Terminology
Example 1:
• a) What is the degree of vertex R?
• b) What is the degree of vertex O?
• c) How many vertices does the graph contain?
• d) How many components does the graph have?
• e) Is this a simple graph?
Example 1:
• a) What is the degree of vertex R? 4
• b) What is the degree of vertex O? 0
• c) How many vertices does the graph contain? 7
• d) How many components does the graph have? 2
• e) Is this a simple graph? Yes
Example 2:
• a) Construct a simple graph, that has no circuits, that has
vertices T,U,V,W,X,Y such that the degree of W is 3 and
there are 2 disjoint parts of the graph.
• b) What are the edges of this simple graph?
Simple Graph
• One possible graph has edges “VW, UW, XW, TY”
(Answers may vary)
Example 3:
Which of the following degree sequences are possible for a simple
graph?
A) {1,2,3,4}
B) {6,4,1,1,2}
C) {1,2,2,3}
D) {2,2,2,2}
Simple Graph
• A) This is not possible because there will be repeating
edges. In a simple graph with 4 vertices, the highest degree
possible is 3.
• B) This is not possible because there will be repeating
edges. In a simple graph with 5 vertices, the highest degree
possible is 4.
• C)This IS a Simple Graph
• D) This Is a Simple Graph
One to One Correspondences
• We say two graphs are the same graph represented
differently if we can find a one-to-one correspondence
between the vertices and edges of the two graphs.
Edges
02 04 05 14 15 23 24 45
↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
AF AB AD CB CD FE FB BD
Vertices
0 A
1 C
2 F
3 E
4 B
5 D
Example 4:
Which of the graphs below is the above graph represented
differently?
A B C
Similar Graphs
• The answer is Graph A because points A,B,C,D,E,F and G
correspond with points W,V,Z,X,U,Y, and T for graph A.
A = 4
B = 5
C = 4
D = 4
E = 2
F = 3
G = 4
W = 4
V = 5
Z = 4
X = 4
U = 2
Y = 3
Z = 4
Degrees
Terminology
*Connected graphs are also called networks.
Terminology
• Example:
(solution on next slide)
Terminology
• Example:
Odd: B and C
Even: A, D, E, and F
(Zero edges is considered “even”)
Example 5:
• Which of the following degree sequences are possible for a
simple graph?*HINT: How many degrees does each edge add to the graph?
• A) (4, 4, 3, 4, 5, 2, 2, 1, 5)
• B) (2, 2, 3, 5, 6, 4, 4, 1)
• C) (3, 3, 4, 3, 5, 4, 2, 3, 1)
• D) (2, 3, 1, 3, 2, 3, 5)
Simple and Connected Graphs
• Answer: A & C
• Why? Because A & C add up to even numbers…OR…A &
C are the ones that have an even number of odd degrees.
• (For a graph to be both simple and connected, each edge
will touch a vertex, and each edge has 2 ends, therefore the
sum of the degrees of every vertex will always be 2 times
the number of edges, and therefore, will always be an even
number.)
Euler’s Theorem
• Example:Find some paths in the graph to the right.
Try finding an Euler path.
• Solution:
Path ACEB has length 3.
Path ACEBDA is an Euler path of length 5. It is also an Euler circuit.
Criteria for Euler Paths & Circuits
• If a graph has more than two vertices of odd degree
then it has no Euler paths.
• If a graph is connected and has zero or exactly two
vertices of odd degree, then it has at least one Euler
path. Any such path must start at one of the odd degree
vertices and end at the other if the graph has two odd
degree vertices, otherwise it must start and end at the
same vertex.
• If a connected graph has zero vertices of odd degree,
then it has at least one Euler circuit.
Does a Graph Contain an Euler Path or Circuit?
* As long as the graph is connected.
# Odd
Vertices
Euler
Path?
Euler
Circuit?
0
2
4, 6, 8 . . .
*YES
*YES
No
*YES
No
No
1, 3, 5 . . . No Such Graphs Exist!
Fleury’s Algorithm: (For finding Euler Circuits)
Fleury’s Algorithm
Fleury’s Algorithm
Example 7:
• Which of the following graphs have Euler circuits?
A B C
Euler Circuits
• Only Graph B has an Euler Circuit. A has odd vertices, and
C is disconnected.
• Use Fleury’s Algorithm to find one of the possible Euler
Circuits (hint: sometimes it is easier to start with the vertices
with the smallest degrees first so you don’t get stuck)
Eulerizing a Graph: (Making it so that it
contains an Euler Circuit)
• Remember you cannot create new edges,
only repeat existing edges.
• Start by connecting vertices with odd degree,
keep repeating edges until all the degrees
are even
Eulerizing a Graph
*Answers may vary
Hamilton paths
Example: Find a
Hamiltonian path.
Complete Graphs:
Finding Hamilton Circuits
*Notice that ABCDA is the reverse of ADCBA
Example 8:
How many Hamilton circuits are there in a
complete graph with 4 vertices?Answer) (n – 1)! = 3! = 6
How many Hamilton Circuits are there in a
complete graph with 5 vertices, if you don’t
count reversals?
Answer) (n – 1)! / 2 = 4!/2 = 24/2 = 12
Example 9:
• Which of the following graphs have Hamiltonian Circuits?
A B C
Hamiltonian Circuits
• Only graph A has a Hamiltonian Circuit in it. We found it by visiting all the VERTICES.
• Note that in order to visit all vertices and start and end at the same vertex, the graph must be connected, and it must not have any bridges
or vertices with degree 1.
Konigsberg Bridge Problem
Starting at some point, can you visit all parts of the city,
crossing each bridge once and only once, and return to the
starting point?
Konigsberg Bridge Problem
This problem is an example of an Euler Circuit, since
we want to cross each bridge once and only once,
and start and stop at the same point.
General Rule:
• Hamiltonian Paths and Circuits require that every
VERTEX be visited; (such as when traveling to
several cities before returning home).
• Eulerian Paths and Circuits require that every
EDGE be used; (such as when designing a delivery
route or deciding which order to plow the streets).
Weighted Graphs
• Example
– The Traveling Salesperson Problem: The
vertices of the graph represent cities, and the
edges of the graph represent the travel cost
between cities.
Traveling Salesperson Problem
• Example: Use the weighted graph to find the
sequence of cities for Danielle to visit that will
minimize her total travel cost.
Solving the TSP by Brute Force
• Solution: Use brute force to explore all
possible circuits:
– List all the Hamiltonian circuits in the graph
– Find the weight of each circuit
– The circuits with the smallest weights give the
solution
PAMCNP and PNCMAP have the smallest
weights, so these two trips would minimize
Danielle’s total travel costs.
Traveling Salesman Problem - Nearest
Neighbor
Step 1: Start at any vertex X.
Step 2: Choose any one edge connected to X that has the
smallest weight. (There may be several with the smallest weight)
Select the vertex at the end of this edge as the next vertex in your
circuit. This vertex is called the nearest neighbor of X.
Step 3: Choose subsequent new vertices as you did in step 2.
(i.e., at every step, choose the next nearest neighbor that hasn’t
already been chosen.
Step 4: After all vertices have been chosen, close the circuit by
returning to the starting vertex.
Example 10:
• Use the nearest neighbor algorithm to find a Hamiltonian circuit starting at vertex K.
• A. List the vertices in the Hamiltonian circuit in the order they are visited. Do not forget to include the starting vertex at both ends.
• B. What is the total weight along the Hamiltonian circuit?
Nearest Neighbor Algorithm
• KH - 14
• HJ - 16
• JI - 19 to include I
• IK - 17 to close the circuit
• The total weight is 66
Traveling Salesman Problem: Best
Edge or Side Sorted AlgorithmStep 1: Choose any edge with the smallest weight.
Step 2: Choose any remaining edge in the graph with the smallest weight
Step 3: Keep adding the edge with the next smallest weight while
following these conditions:
Do not form a circuit before all the vertices have been added
Do not add an edge that would give a vertex degree three
**With this algorithm, you may start at any vertex, so choose the one that is
connected to an edge with the smallest weight. You can rewrite your circuit to
start with a specific vertex at the end.
* Remember ABCDEA is the same circuit as CDEABC
Example 11:
• Use the Best Edge Algorithm to find the Hamiltonian circuit
with the smallest weight. List the weights of the edges in
order they were chosen using the algorithm.
Finding a Hamilton Circuit using Best Edge
• SU - 9
• SV - 10
• UV - 11 would close the circuit.
• TS - 12 would make S degree 3
• TU - 13
• TV - 14 would close the circuit
• SW - 15 would make S degree 3
• UW - 16 would make U degree 3
• VW - 17
• TW – 18 to close the circuit.
• Answer: 9, 10, 13, 17, 18
Finding a Hamilton Circuit Using Side-Sorted
A B C D E
A - 9 8 5 13
B 9 - 12 4 6
C 8 12 - 7 10
D 5 4 7 - 17
E 13 6 10 17 -
• BD - 4
• AD - 5
• BE - 6
• CD - 7 would make D degree 3
• AC - 8
• CE - 10 to close the circuit
• The total weight is 33
Trees
• For a graph to be a tree, it must be connected
and have no circuits.
• A forest is collection of one or more trees.
Spanning Tree
A spanning tree is a subgraph that…• Contains all the original vertices in the graph,
• Is connected,
• Contains no circuits.
Trees and Forests
• A forest is a collection of trees. Which of the following are trees and which are forests?
A B C D
Tree/Forest
• Graph A is a Tree and a forest
• Graph B is a forest
• Graph C has a circuit, EHGDE, so it is NOT a tree or a forest.
• Graph D has a circuit, KJNMK, so it is NOT a tree or a forest.
• (Hint: A tree can be a forest but a forest doesn’t have to be a tree)
A) B)
Kruskal’s Algorithm
- Used to find a minimal spanning tree of a connected
weighted graph.
Step 1: Choose any edge with the smallest weight.
Step 2: Choose any remaining edge in the graph with the
smallest weight.
Step 3: Keep adding the next smallest weight edge that does
not create a circuit until all vertices have been added to your
subgraph.
Example 12:
• Consider the graph given above. Use Kruskal's algorithm to
find the minimum spanning tree.
• A) What is the total weight of the spanning tree?
• B) List the weights of the selected edges separated by
commas in the order of selection.
Kruskal's Algorithm• IJ - 3
• GI - 4
• HJ - 5
• All originally vertices are
connected, so these edges
form a minimal spanning
tree. • Note: Kruskal’s algorithm does
not find circuits. All that is
required is that all vertices are
included.
Example 13:
• Consider the cities C, D, E, F, G & H. The costs of the
possible roads are given below. Find the cheapest way to
connect the cities. • c(C, F) = 18
• c(D, E) = 13
• c(D, F) = 17
• c(D, H) = 12
• c(E, F) = 16
• c(E, G) = 11
• c(F, G) = 14
• c(F, H) = 15
• c(G, H) = 10
Example 13:
• Consider the cities C, D, E, F, G & H. The costs of the
possible roads are given below. Find the cheapest way to
connect the cities. • c(C, F) = 18
• c(D, E) = 13
• c(D, F) = 17
• c(D, H) = 12
• c(E, F) = 16
• c(E, G) = 11
• c(F, G) = 14
• c(F, H) = 15
• c(G, H) = 10
Answer: 10 + 11 + 12 + 14 + 18 = 65
• GH - 10
• EG - 11
• DH - 12
• DE - 13 would create a circuit
• FG - 14
• CF - 18 only missing vertex C
Thank You For Coming!
• Tutoring help for this course is always available in the
Center for Academic Support.
• We are open from 8 am to 7 pm Monday through Thursday,
and 8 am to 3 pm on Friday. We are closed Saturday and
Sunday.
• Walk in anytime or call the front desk to make an
appointment: 271-4524.