Graph Theory overview

advertisement
Graph Theory
Topics to be covered:
•Use graphs to model and solve problems such as shortest paths, vertex
coloring, critical paths, routing, and scheduling problems
•Convert from a graph to an adjacency matrix and vice versa.
•Use directed graphs, spanning tress, rooted trees, binary trees, or decision
trees to solve problems.
•Demonstrate understanding of algorithms such as depth-first and breadthfirst walk of a tree or maximal matching.
•Use matching or bin-packing techniques to solve optimization and other
problems.
•Compare and contrast different graph algorithms in terms of efficiency
and types of problems that can be solved.
Basic Terminology
• A graph consists of a finite set of vertices and
edges.
Vertex
Parallel
edges
Loop
Degrees of Vertices
• The degree of a vertex is determined by the number of edges it is
connected to.
• If all the vertices of a graph have the same degree, then it is a
regular graph.
• The handshake theorem says that the sum of the degree of all the
vertices in graph G is equal to twice the number of edges in G
• A corollary of this theorem is that the total degree of a graph is
even.
• This is an example of a
regular graph. It is also a
complete graph, because
every vertex is connected to
every other vertex.
Adjacency Matrixes
• Every graph can be represented by an nxn
matrix, where n is the number of vertices in
the graph.
• For the matrix A = (ai,j) if ai,j = 1 then there is
an edge from vertex i to vertex j. If 0 then
there is no edge.
Adjacency Matrix Example
Vertex Coloring
• To obtain the proper coloring of a graph you must color
each vertex so that it is a different color from all
adjacent vertices.
• The least number of colors possible to color a graph is
called its chromatic number.
• We use the notation X(G) for the chromatic number of
graph G.
• Unplugged activity:
http://www.csunplugged.org/graph-colouring
A properly colored graph with a
chromatic number of 2.
Applications of Vertex Coloring
• Graph coloring can be used to solve problems like scheduling.
• Ex. Suppose you want to schedule final exams and, being very
considerate, you want to avoid having a student do more than one exam a
day. We shall call the courses 1,2,3,4,5,6,7 . In the table below a star in
entry i,j means that course i and j have at least one student in common so
you can't have them on the same day. What is the least number of days
you need to schedule all the exams? Show how you would schedule the
exams.
.
1 2 3 4 5 6 7
1
.
*
*
*
-
*
*
2
*
.
*
-
-
-
*
3
*
*
.
*
-
-
-
4
*
-
*
.
*
*
-
5
-
-
-
*
.
*
-
6
*
-
-
*
*
.
*
7
*
*
-
-
-
*
.
Applications of Vertex Coloring
• To solve the problem
mentioned previously
you could use that
table as an adjacency
matrix to construct a
graph like the one
shown here
• This graph can be
colored with a
minimum of 4 colors
Paths and Circuits Definitions
• Let G be a graph, and v and w be vertices in G.
• A walk from v to w is a finite series of adjacent vertices and
edges of G.
• A path from v to w is a walk from v to w that does not
contain a repeated edge.
• A simple path from v to w is a path that does not contain a
repeated vertex.
• A closed walk is a walk that starts and ends at the same
vertex.
• A circuit is a closed walk that does not contain a repeated
edge.
• A simple circuit is a circuit that does not have any other
repeated vertex except for the first and last.
Paths and Circuits Definitions
Continued
Repeated Edge? Repeated Vertex? Starts and ends
at same point?
Walk
Allowed
Allowed
Allowed
Path
No
Allowed
Allowed
Simple Path
No
No
No
Closed Walk
Allowed
Allowed
Yes
Circuit
No
Allowed
Yes
Simple Circuit
No
First and Last
Only
Yes
Euler Circuits and Paths
• An Euler Circuit for graph G is a circuit that contains every
vertex and every edge of G.
• If a graph has an Euler circuit, then every vertex of that
graph has an even degree.
• If any vertex of a graph has an odd degree then that graph
does not have an Euler circuit.
• If every vertex of a nonempty graph has an even degree
and if the graph is connected then the graph has an Euler
circuit.
• An Euler Path from v to w for graph G is a path from v to w
that passes through every vertex at least once, and
traverses every edge exactly once.
• There is an Euler path from v to w if, and only if, v and w
have both even or odd degrees, and all other vertices of G
have an even degree.
Application of an Euler Path
The most famous Euler Circuit was that of the
7 bridges of Königsberg. This was when Euler
created the field of graph theory.
This problem is described as follows:
You must find a walk through the city that
crossed each bridge once and only once.
The islands could not be reached by any
route other than bridges, and each bridge
must be crossed completely every time.
Euler simplified the problem into this graphical
representation. This graph makes it clear that
all 4 vertices have odd degrees. This makes it
impossible for an Euler path to be created.
Hamiltonian Circuits
• A Hamiltonian circuit for graph G is a simple circuit that
includes every vertex of G.
• Note that edges can be omitted, and often are in the
optimal solution.
• If graph G has a nontrivial Hamiltonian circuit, then G has a
subgraph H with the following properties:
1.
2.
3.
4.
•
H contains every vertex of G.
H is connected.
H has the same number of edges and vertices.
Every vertex of H has degree 2.
Therefore, if graph G does not have a subgraph H with
those properties, then graph G does not have a
Hamiltonian circuit.
Application of Hamiltonian Circuits:
The Traveling Salesman Problem
In the Traveling Salesman Problem you must
visit each city exactly once, starting and ending
in city A. Which route can you take to minimize
the total distance traveled?
This problem can be solved by
writing all of the possible
Hamiltonian Circuits:
Route
Total Distance
A B C D A 30 + 30 + 25 + 40 = 125
A B D C A 30 + 35 + 25 + 50 = 140
A C B D A 50 + 30 + 35 + 40 = 155
ACDBA
140
ADBCA
155
ADCBA
125
Thus either route A B C D A or route
A D C B A gives a minimum total
distance of 125.
NP-Complete Problems
• The general traveling salesman problem involves finding a
Hamiltonian circuit to minimize the total distance traveled for a
graph with n vertices in which each edge is marked with a distance.
• One way to solve it is the method used above; that of listing out all
possible circuits.
• This becomes impossible with larger graphs, though:
– For a complete graph with 30 vertices, there would be 29! = 8.84 x
1030 different circuits.
– Even if a circuit could be found and computed in 1 nanosecond, it
would still take 2.8 x 1014 years to finish the computation.
• This problem is an example of an NP-complete problem – a set of
problems that take a very long time to solve, but have answers that
can be verified quickly.
• However, there are efficient algorithms that find “pretty good”
solutions in a reasonable amount of time.
Finding the Shortest Path
• Edges on graphs can be given
weights representing the cost to
travel from one vertex to
another.
• There are a number of
algorithms to find the shortest
path on a weighted graph, the
most notable of which is
Dijkstra’s algorithm.
Dijkstra’s Algorithm
• The goal of this algorithm is to find the shortest path
between two vertices on a graph.
• Once the distance from the start point to a vertex has been
calculated, that vertex is considered marked.
• For the current node it will consider all adjacent unmarked
nodes and determine their distance from the initial node.
• If it is less than the previously recorded distance, overwrite
it.
• Once all adjacent nodes have been considered the current
node is marked and the algorithm continues to the next
node with the shortest distance.
• This process repeats until the terminal vertex has been
found.
Example of Dijkstra’s Algorithm
Trees
• A graph is a tree if, and only if, it has no loops
and is connected.
• A rooted tree is a tree in which one vertex is
distinguished from the others and is called the
root.
• A Binary tree is a rooted tree in which every
parent has at most two children. Each child is
either a left child or a right child.
Binary Trees
• Binary trees are used to represent algebraic
expressions in computers:
• ((a – b) * c) + (d/e):
+
*
–
a
/
c
b
d
e
Spanning Trees
• A spanning tree of graph G is a subgraph of G
that contains every vertex of G and is a tree.
• A minimum spanning tree is a spanning tree
for which the sum of the weights of all the
edges is as small as possible.
• Unplugged Activity:
http://www.csunplugged.org/minimalspanning-trees
Download