Euler Trails and Graph Algorithms
Euler Trails and Graph Algorithms
SYLLABUS
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Euler Circuits
Leonhard Euler first discussed and used Euler paths and circuits in 1736.
Rather than finding a minimum spanning tree that visits every vertex of a graph, an
Euler path or circuit can be used to find a way to visit every edge of a graph once
and only once.
By counting the number of vertices of a graph, and their degree we can determine
whether a graph has an Euler path or circuit.
classifications and differentiation of graphs according to the connections
between nodes. Don’t write or
place any image
how the edges relate with the nodes, forming specific sequences.
[Link] in this area
mathforliberalarts/chapter/introduction-euler-paths/
Dr. D Khalandar Basha
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Walk
When we have a graph and traverse it, then that traverse will be known as a walk.
A walk is a sequence of vertices so that there is an edge between consecutive
vertices.
A walk can repeat vertices and edges.
A walk can be defined as a sequence of edges and vertices of a graph.
The number of edges which is covered in a walk will be known as the Length of the
walk.
In a graph, there can be more than one walk.
Don’t write or
So for a walk, the following two points are important, place any image
in this area
Edges can be repeated Vertex can be repeated
Dr. D Khalandar Basha
Walk
For example: In this example, we have a graph, which is described as follows:
In the above graph, there can be many walks, but some of them
are described as follows:
1. A, B, C, E, D (Number of length = 4)
2. D, B, A, C, E, D, C (Number of length = 7)
3. E, C, B, A, C, E, D (Number of length = 6)
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Walk
Types of Walks
There are two types of the walk, which are described as follows:
Open walk
Closed walk
Open Walk:
A walk will be known as an open walk in the graph theory if the vertices at which
the walk starts and ends are different.
That means for an open walk, the starting vertex and ending vertex
Don’t write or
must be different. place any image
in this area
In an open walk, the length of the walk must be more than 0.
Dr. D Khalandar Basha
Walk
Closed Walk:
A walk will be known as a closed walk in the graph theory if the vertices at which
the walk starts and ends are identical.
That means for a closed walk, the starting vertex and ending vertex must be the
same.
In a closed walk, the length of the walk must be more than 0.
The walk will be known as the Trivial walk if length of the walk = 0.
Don’t write or
In case of the open walk and closed walk, the edges and vertices can be place any image
in this area
repeated.
Dr. D Khalandar Basha
Walk
Suppose there is a graph, which is described as follows:
In this graph, there is also a closed walk and an
open walk, which are described as follows:
Closed walk = A, B, C, D, E, C, A
Open walk = A, B, C, D, E, C
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Trails
A trail is a walk with no repeated edges.
A trail can repeat vertices but not edges.
A trail can be described as an open walk where no edge is allowed to repeat.
So for a trail, the following point is important, which is described as follows:
Vertex can be repeated
But edges are not allowed to repeat.
An open trail starts and ends on different vertices.
A closed trail (circuit) starts and ends on the same vertex.
Don’t write or
All trails are walks and all circuits are closed walks place any image
in this area
Dr. D Khalandar Basha
Trails
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Circuit
A circuit is a closed trail; that is, a trail that starts and ends at the same vertex with
no repeated edges though vertices may be repeated.
A circuit can be described as a closed walk where no edge is allowed to repeat.
In the circuit, the vertex can be repeated.
A closed trail in the graph theory is also known as a circuit.
So for a circuit, the following two points are important, which are
Edges cannot be repeated
Vertex can be repeated
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Circuit
In the above graph, there is a circuit, which is described
as follows:
Circuit: A, B, D, C, F, H, C, A
Node_1 -> Node_2 -> Node_3 -> Node_4 -> Node_5 -> Node_3
-> Node_1
is an example of a circuit as no edges are repeated but has
vertices repeated.
Dr. D Khalandar Basha
Path
A path is a trail with no repeated vertex (or edges).
A path on n vertices is denoted Pn.
In graph theory, a path is defined as an open walk in which-
Neither vertices (except possibly the starting and ending vertices) are
allowed to repeat.
Nor edges are allowed to repeat.
Don’t write or
The walk abcde does not repeat any edges. place any image
in this area
Dr. D Khalandar Basha
Cycle
A closed path in the graph theory is also known as a Cycle.
A cycle is a type of closed walk where neither edges nor vertices are allowed to
repeat.
There is a possibility that only the starting vertex and ending vertex are the same
in a cycle.
So for a cycle, the following two points are important, as follows:
Edges cannot be repeated
Vertex cannot be repeated
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Cycle
A closed path in the graph theory is also known as a Cycle.
Traversing a graph such that do not repeat a vertex nor repeat a edge but the
starting and ending vertex must be same
A cycle on n vertices is denoted Cn.
A cycle is a type of closed walk where neither edges nor vertices are allowed to
repeat.
The possibility in a cycle is the starting and ending vertex are the same in a cycle.
So for a cycle, the following two points are important,
Don’t write or
Edges cannot be repeated place any image
in this area
Vertex cannot be repeated
Dr. D Khalandar Basha
Cycle
Example of a circuit.
Notice how no edges are repeated in the walk bcgfb,
which makes it definitely a trail, and that the start and
end vertex b is the same which makes it closed.
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Summary
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Summary
Summary
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Summary
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Problems
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Problems
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Problems
finding the best (fastest, shortest, cheapest) route between two destinations from
A to B
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Problems
The following map shows all possible routes in Mohamad's town.
Mohamad travels from his home to his friend's house on a daily basis.
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Problems
The graph below shows the time in hours taken to travel between various country
towns.
Determine the least amount of time needed to
travel between towns P and X.
State the route that would achieve this by listing
the vertices in order, separated by commas.
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Problems
The graph below shows the time in hours taken to travel between various country
towns.
Determine the least amount of time needed to
travel between towns P and X.
State the route that would achieve this by listing
the vertices in order, separated by commas.
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Problems
Example: Given the graph below, find a trail (that is not a path) from a to c, a path
from a to c, a circuit (that is not a cycle) starting at b, and a cycle starting at b.
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Problems
The order in which the edges will be traveled are noted in the following routes.
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Connected
Example: Island City has two islands, a peninsula, and left and right banks, as shown
Don’t write or
It would be impossible to travel every bridge and thenreturn home. place any image
In particular, there is no way to even travel from one side of the river to in this area
the other!
Dr. D Khalandar Basha
Connected
Theorem Every x − y walk contains an x − y path.
Let the length L of an x − y walk W.
For the base case, if L = 0 then the walk does not have any edges and so contains a
singe vertex, that is x = y and so W is a path of length 0.
Next assume L ≥ 1 and suppose the all walks of length k < L. If W has no repeated
vertex, then it cannot have any repeated edges and so W is an x−y path.
Otherwise W has a repeated vertex, call it u.
Then remove all edges and vertices between two appearances of u
Don’t write or
in the walk, and leave only one copy of u. place any image
in this area
Dr. D Khalandar Basha
Connected
This will produce a shorter x − y walk W which is contained in W.
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Connected
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Maximum vs maximal
An object X is maximum if it is the largest among all objects under consideration;
that is, |X| ≥ |A| for all A ∈ U.
An object X is maximal if it cannot be made larger.
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Maximal, Maximum
The path b c d e f (highlighted on the left) is maximal because we cannot add any
additional vertices and keep it a path.
Don’t write or
However, it is not maximum since a longer path can be found, namely place any image
in this area
h g c d e f shown on the right.
Dr. D Khalandar Basha
Don’t write or
place any image
in this area
Then the edge xy completes a cycle with the portion of P from x to y.
Dr. D Khalandar Basha
Eulerian Graphs
Let G be a graph.
An eulerian circuit (or trail) is a circuit (or trail) that contains every edge and
every vertex of G.
If G contains an eulerian circuit it is called eulerian and if G contains an eulerian
trail but not an eulerian circuit it is called semi-eulerian
Eulerian Graphs
If a vertex is odd, then there is no pairing available and we would eventually get
stuck at that vertex.
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Eulerian Graphs
Note
Let G be a graph.
An eulerian circuit (or trail) is a circuit (or trail) that contains every edge and
every vertex of G.
If G contains an eulerian circuit it is called eulerian and
if G contains an eulerian trail but not an eulerian circuit it is called semi-eulerian.
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Eulerian Graphs
Note
A graph G is eulerian if and only if
(i) G is connected and
(ii) every vertex has even degree.
Eulerian Graphs
Example Consider the graphs, Which ones are eulerian? semi-eulerian? neither?
Solution:
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Eulerian Graphs
Example Consider the graphs, Which ones are eulerian? semi-eulerian? neither?
Solution:
• Even though the graph is connected, it is neither eulerian nor semi-eulerian since
it has more than two odd vertices (namely, a, b, e, and f).
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Eulerian Graphs
Example Consider the graphs, Which ones are eulerian? semi-eulerian? neither?
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Eulerian Graphs
Example Consider the graphs, Which ones are eulerian? semi-eulerian? neither?
• The graph representing Island City in Example is not connected,
so it is neither eulerian nor semi-eulerian.
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Eulerian Graphs
Example Consider the graphs, Which ones are eulerian? semi-eulerian? neither?
The graph representing K¨onigsberg is neither eulerian nor semieulerian since all
four vertices are odd.
Eulerian Graphs
Example Consider the graphs, Which ones are eulerian? semi-eulerian? neither?
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Eulerian Graphs
Example Consider the graphs, Which ones are eulerian? semi-eulerian? neither?
The graph in Example is neither eulerian nor semi-eulerian since it is not
connected.
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Eulerian Graphs
Example Consider the graphs, Which ones are eulerian? semi-eulerian? neither?
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Eulerian Graphs
Example Consider the graphs, Which ones are eulerian? semi-eulerian? neither?
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Eulerian Graphs
Example Consider the graphs, Which ones are eulerian? semi-eulerian? neither?
The graph in Example is semi-eulerian since it is connected and exactly two
vertices are odd.
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Euler Circuits
EULER PATH
An Euler path is a path that uses every edge in a graph with no repeats.
Being a path, it does not have to return to the starting vertex.
EULER CIRCUIT
An Euler circuit is a circuit that uses every edge in a graph with no repeats.
Being a circuit, it must start and end at the same vertex.
EULER’S PATH AND CIRCUIT THEOREMS
A graph will contain an Euler path if it contains at most two vertices of odd degree.
A graph will contain an Euler circuit if all vertices have even degree
Dr. D Khalandar Basha
Euler Circuits
EXAMPLE
In the graph shown below, there are several Euler paths.
Euler Circuits
EXAMPLE
The graph below has several possible Euler circuits.
Euler Circuits
Don’t write or
place any image
One Euler path for the above graph is F, A, B, C, F, E, C, D, E in this area
Dr. D Khalandar Basha
Euler Circuits
This Euler path travels every edge once and only once and starts and ends at
different vertices.
This graph cannot have an Euler circuit since no Euler path can start and end at
the same vertex without crossing over at least one edge more than once.
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Euler Circuits
This Euler path travels every edge once and only once and starts and Don’t write or
place any image
ends at the same vertex. Therefore, it is also an Euler circuit in this area
Dr. D Khalandar Basha
Euler Circuits
Euler’s Theorem 1 :
If a graph has any vertices of odd degree, then it cannot have an Euler circuit.
If a graph is connected and every vertex has an even degree, then it has at least
one Euler circuit (usually more).
Euler’s Theorem 2 : If a graph has more than two vertices of odd degree, then it
cannot have an Euler path.
If a graph is connected and has exactly two vertices of odd degree, then
it has at least one Euler path (usually more). Any such path must start Don’t write or
place any image
at one of the odd-degree vertices and end at the other one. in this area
Dr. D Khalandar Basha
Euler Circuits
Euler’s Theorem 3 :
The sum of the degrees of all the vertices of a graph equals twice the number of
edges (and therefore must be an even number).
Therefore, the number of vertices of odd degree must be even.
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Euler Circuits
Euler Circuits
Ex. Finding Euler Circuits:
The graph shown has an Euler circuit since each
vertex in the entire graph is even degree.
Thus, start at one even vertex, travel over each
vertex once and only once, and end at the starting
point.
One example of an Euler circuit for this graph is A, E, A, B, C, B, E, C, D, E, F, D, F, A.
This is a circuit that travels over every edge once and only once and
Don’t write or
starts and ends in the same place. place any image
in this area
There are other Euler circuits for this graph.
Dr. D Khalandar Basha
Euler Circuits
EULERIAN PATH
An Eulerian path is a path of edges that visit all edges in a graph exactly once.
We can find an Eulerian path on the graph below only if we start at specific nodes.
Don’t write or
place any image
[Link] in this area
But, if the starting point is changed we might not get the desired result.
mathforliberalarts/chapter/introduction-euler-paths/
Dr. D Khalandar Basha
Euler Circuits
EULERIAN CIRCUIT
An Eulerian circuit is an Eulerian path that starts and ends at the same vertex.
Euler Circuits
EULERIAN CIRCUIT
To support our above statement let’s take another example. This is basically the
same but with some modification. We start from the same point.
Don’t write or
place any image
[Link] in this area
mathforliberalarts/chapter/introduction-euler-paths/
Dr. D Khalandar Basha
Euler Circuits
The conditions required for a valid Eulerian path/circuit:
UNDIRECTED GRAPH:
Eulerian circuit - every vertex has an even degree.
Eulerian path - either every vertex has an even degree or exactly two vertices have
an odd degree.
DIRECTED GRAPH:
Eulerian circuit - every vertex has equal indegree and outdegree.
Eulerian path - at most one vertex has (outdegree) - (indegree) = 1 and
Don’t write or
at most one vertex has (indegree) - (outdegree) = 1, and all other place any image
in this area
vertices have equal in and outdegrees.
Dr. D Khalandar Basha
Euler Circuits
FINDING AN EULERIAN PATH (DIRECTED GRAPH)
Step one to finding an Eulerian path is determining if an Eulerian path even exists.
Recall that for an Eulerian path to exist, at most one vertex has (outdegree) -
(indegree) = 1 and at most one vertex has (indegree) - (outdegree) = 1, and all
other vertices have equal in and outdegrees.
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Euler Circuits
In our above example we have calculated both in and outdegree of each node, and
now we can confirm our conditions for a Eulerian path. No nodes have out[i] - in[i]
> 1 or in[i] - out[i] > 1 and there are just the right amount of start/end nodes.
Node one is the only node with exactly one extra outgoing edge, so it’s our only
valid start node. Similarly, node six is the only node with exactly one extra
incoming edge, so it will end up being the end node.
Don’t write or
place any image
[Link]
in this area
circuit-in-graphs
Dr. D Khalandar Basha
Algorithms
how do we find Eulerian or semi-eulerian graph
Each algorithms will be described in terms of the input, steps to perform, and output
Methods for finding an eulerian circuit
Fleury’s Algorithm (easiest to walk through )
Fleury’s Algorithm will produce either an eulerian circuit or an eulerian
trail depending on which solution is possible.
Hierholzer’s Algorithm
algorithm begins by finding an arbitrary circuit originating
Don’t write or
from the starting vertex. If this circuit contains all the edges of place any image
in this area
the graph, then an eulerian circuit has been found.
Dr. D Khalandar Basha
Fleury’s Algorithm
Input: Connected graph G where zero or two vertices are odd.
Steps:
1. Choose a starting vertex, call it v.
If G has no odd vertices, then any vertex can be the starting point.
If G has exactly two odd vertices, then v must be one of the odd vertices.
2. Choose an edge incident to v that is unlabeled and label it with the number in
which it was chosen, ensuring that the graph consisting of unlabeled edges remains
connected.
3. Travel along the edge to its other endpoint. Don’t write or
place any image
4. Repeat Steps (2) and (3) until all edges have been labeled. in this area
Fleury’s Algorithm
The intention behind Fleury’s Algorithm is that you are prevented from getting stuck
at a vertex with no edges left to travel.
In practice, it may be helpful to use two copies of the graph—one to keep track of the
route and the other where labeled edges are removed.
This second copy makes it easier to see which edges are unavailable to be chosen.
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Fleury’s Algorithm
Example Input: A connected graph (shown below) where every vertex has even
degree. We are looking for an eulerian circuit.
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Fleury’s Algorithm
Step 1: Since no starting vertex is explicitly stated, choose vertex v as starting vertex.
Step 2: We can choose any edge incident to v. Here we chose vx. The labeled graph is
on the left and the unlabeled portions are shown on the right with edges removed
that have already been chosen.
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Fleury’s Algorithm
Step 3: Looking at the graph to the right, we can choose any edge out of x.
Here we chose xy. The labeled and unlabeled graphs have been updated below.
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Fleury’s Algorithm
Step 4: At this point we cannot choose yv, as its removal would disconnect the
unlabeled graph shown on the right in Step 3. However, yx and yz are both valid
choices. Here we chose yx.
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Fleury’s Algorithm
Step 5: There is only one available edge xz.
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Fleury’s Algorithm
Step 6: There is only one available edge zy.
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Fleury’s Algorithm
Step 7: There is only one available edge yv.
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Fleury’s Algorithm
Step 8: Both vw and vu are valid choices for the next edge. Here we chose vw.
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Fleury’s Algorithm
Step 9: There is only one available edge wu.
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Fleury’s Algorithm
Step 10: There is only one available edge uv.
Don’t write or
Output: The graph above on the left has an eulerian circuit labeled, place any image
in this area
starting and ending at vertex v.
Dr. D Khalandar Basha
Fleury’s Algorithm
Does the graph below have an Euler Circuit? If so, find one.
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Fleury’s Algorithm
Does the graph below have an Euler Circuit? If so, find one.
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Hamiltonian cycle
When a graph would contain an eulerian circuit, that must travel through every
edge and vertex
In graph theoretic terms, we are looking for a tour through the graph that hits
every vertex exactly once.
What type of tour is this?
If the start and end at the same location, we are searching for a cycle.
If the starting and ending points can differ, we are searching for a path.
Dr. D Khalandar Basha
Hamiltonian cycle
Definition :
A cycle in a graph G that contains every vertex of G is called a hamiltonian cycle.
A path that contains every vertex is called a hamiltonian path.
A graph that contains a hamiltonian cycle is called hamiltonian.
The Hamiltonian cycles and paths travel through every vertex exactly once.
Hamiltonian cycle
Definition :
A cycle in a graph G that contains every vertex of G is called a hamiltonian cycle.
A path that contains every vertex is called a hamiltonian path.
A graph that contains a hamiltonian cycle is called hamiltonian.
The Hamiltonian cycles and paths travel through every vertex exactly once.
Hamiltonian cycle
Hamiltonian cycle
Hamiltonian cycle
Example For each of the graphs below, determine if they have hamiltonian cycles
(and paths) and eulerian circuits (and trails).
Hamiltonian cycle
Example For each of the graphs below, determine if they have hamiltonian cycles
(and paths) and eulerian circuits (and trails).
Hamiltonian cycle
Example For each of the graphs below, determine if they have hamiltonian cycles
(and paths) and eulerian circuits (and trails).
Hamiltonian cycle
Example For each of the graphs below, determine if they have hamiltonian cycles
(and paths) and eulerian circuits (and trails).
Hamiltonian cycle
Example For each of the graphs below, determine if they have hamiltonian cycles
(and paths) and eulerian circuits (and trails).
Hamiltonian cycle
If a graph has a hamiltonian path, it may or may not have a hamiltonian cycle.
Dr. D Khalandar Basha
Hamiltonian cycle
Hamiltonian cycle
Example : Use the properties to show that the graphs are not hamiltonian.
Hamiltonian cycle
Dr. D Khalandar Basha
Ore’s Theorem
Let G be a graph with n ≥ 3 vertices.
If deg(x) + deg(y) ≥ n for all distinct nonadjacent vertices, then G has a hamiltonian
cycle.
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
A traveling salesman has customers in numerous cities; he must visit each of them
and return home, but wishes to do this with the least total cost; determine the
cheapest route possible for the salesman.
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Steps:
1. Choose a starting vertex, call it v.
2. Find all hamiltonian cycles starting at v. Calculate the total weight of each cycle.
3. Compare all (n−1)! cycles. Pick one with the least total weight. (Note: there
should be at least two options).
Don’t write or
Output: Minimum hamiltonian cycle. place any image
in this area
Dr. D Khalandar Basha
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Don’t write or
Output: The circuit is a e b c d a with total weight 1365 place any image
in this area
Dr. D Khalandar Basha
Steps:
1. Choose a starting vertex, call it v.
2. Apply the Nearest Neighbor Algorithm.
3. Repeat Steps (1) and (2) so each vertex of Kn serves as the starting vertex.
4. Choose the cycle of least total weight. Rewrite it with the desired reference point.
Don’t write or
Output: hamiltonian cycle. place any image
in this area
Dr. D Khalandar Basha
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Don’t write or
place any image
in this area
Output: The resulting cycle is a e b c d a with total weight 1365.
Dr. D Khalandar Basha
Relative error
The relative error for a solution is given by
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Relative error
The relative error for a solution is given by
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Shortest path
The shortest way to travel between two locations is perhaps one of the oldest
questions.
This problem can be described in graph theoretic terms as the search for a shortest
path on a weighted graph.
Recall that a path is a sequence of vertices in which there is an edge between
consecutive vertices and no vertex is repeated.
As with the algorithms for the Traveling Salesman Problem, the weight associated to
an edge may represent more than just distance (e.g., cost or time) and the
Don’t write or
shortest path really indicates the path of least total weight. place any image
in this area
Dr. D Khalandar Basha
Dijkstra Algorithm
In 1956 Edsger W. Dijkstra proposed the algorithm to study not out of necessity for
finding a shortest route, but rather as a demonstration of the power of a new
“automatic computer” at the Mathematical Centre in Amsterdam.
This algorithm would become in almost every GIS (Geographic Information System,
or mapping software) uses a modification of Dijkstra’s Algorithm to
provide directions.
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Dijkstra Algorithm
Numerous versions of Dijkstra’s Algorithm exist, though two basic descriptions
In one, a shortest path from starting and ending vertex is found.
we will study the more general version that finds the shortest path from a specific
vertex to all other vertices in the graph.
Dijkstra’s Algorithm is a bit more complex than the algorithms.
Each vertex is given a two-part label L(v) = (x,(w(v)).
The first portion of the label is the name of the vertex used to travel to v.
The second part is the weight of the path that was used to get to v from
Don’t write or
the designated starting vertex. place any image
in this area
Dr. D Khalandar Basha
Dijkstra Algorithm
At each stage of the algorithm, consider a set of free vertices, denoted by an F below.
Free vertices are the neighbors of previously visited vertices that are themselves
not yet visited.
The most complex portion of this algorithm is the labeling of the vertices and how
they are updated with iterations of Step.
In the initial step of Dijkstra’s Algorithm, all vertices have no entry in the first part
of the label and the second part is 0 for the starting vertex and ∞ for all others.
Note that the set F of free vertices consists of all neighbors of highlighted
Don’t write or
vertices and all are under consideration for becoming the next highlighted place any image
in this area
vertex.
Dr. D Khalandar Basha
Dijkstra Algorithm
Input: Weighted connected simple graph G = (V, E, w) and designated Start vertex.
Steps:
1. For each vertex x of G, assign a label L(x) so that L(x) = (−, 0) if x = Start and L(x) =
(−, ∞) otherwise. Highlight Start.
2. Let u = Start and define F to be the neighbors of u. Update the labels for each
vertex v in F as follows:
if w(u) + w(uv) < w(v), then redefine L(v) = (u, w(u) + w(uv)) otherwise do not
change L(v)
Don’t write or
3. Highlight the vertex with lowest weight as well as the edge uv used to place any image
in this area
update the label. Redefine u = v.
Dr. D Khalandar Basha
Dijkstra Algorithm
4. Repeat Steps (2) and (3) until each vertex has been reached. In all future
iterations, F consists of the un-highlighted neighbors of all previously highlighted
vertices and the labels are updated only for those vertices that are adjacent to the
last vertex that was highlighted.
5. The shortest path from Start to any other vertex is found by tracing back using
the first component of the labels. The total weight of the path is the weight
given in the second component of the ending vertex.
Don’t write or
Output: Highlighted path from Start to any vertex x of weight w(x). place any image
in this area
Dr. D Khalandar Basha
Dijkstra Algorithm
Example Apply Dijkstra’s Algorithm to the graph below where Start = g.
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Dijkstra Algorithm
Step 1: Highlight g. Define L(g) = (−, 0) and L(x) = (−, ∞) for all x = a, · · · , f.
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Dijkstra Algorithm
Step 2: Let u = g. Then the neighbors of g comprise F = {a, f}. We
Compute w(g) + w(ga) = 0 + 9 = 9 < ∞ = w(a)
w(g) + w(gf) = 0 + 1 = 1 < ∞ = w(f)
Update L(a) = (g, 9) and L(f) = (g, 1). Since the minimum weight for all vertices in F
is that of f, we highlight the edge gf and the vertex f.
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Dijkstra Algorithm
Step 3: Let u = f.
Then the neighbors of all highlighted vertices are F = {a, b, d, e}. We compute
w(f) + w(f a) = 1 + 1 = 2 < 9 = w(a)
w(f) + w(f b) = 1 + 2 = 3 < ∞ = w(b)
w(f) + w(f d) = 1 + 1 = 2 < ∞ = w(d)
w(f) + w(fe) = 1 + 8 = 9 < ∞ = w(e)
Update L(a) = (f, 2), L(b) = (f, 3),
L(d) = (f, 2) and L(e) = (f, 9).
Don’t write or
Since the minimum weight for all vertices in F is that of a or d, we choose place any image
in this area
to highlight the edge f a and the vertex a.
Dr. D Khalandar Basha
Dijkstra Algorithm
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Dijkstra Algorithm
Step 4: Let u = a.
Then the neighbors of all highlighted vertices are
F = {b, d, e}.
Note, updating the label for b since this is the only
vertex adjacent to a, the vertex highlighted in the
previous step.
w(a) + w(ba) = 2 + 6 = 8 ≮ 3 = w(b)
Do not update the label for b since the computation above is not less
Don’t write or
than the current weight of b. place any image
in this area
Dr. D Khalandar Basha
Dijkstra Algorithm
The minimum weight for all vertices in F is that of d, and so we highlight the edge f d
and the vertex d
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Dijkstra Algorithm
Step 5: Let u = d.
Then the neighbors of all highlighted vertices are
F = {b, c, e}. We compute
w(d) + w(dc) = 2 + 5 = 7 < ∞ = w(c)
w(d) + w(de) = 2 + 6 = 8 < 9 = w(e)
Update L(c) = (d, 7) and L(e) = (d, 8).
Since the minimum weight for all vertices in F is that of b, we highlight the edge bf
and the vertex b.
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Dijkstra Algorithm
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Dijkstra Algorithm
Step 6: Let u = b. Then the neighbors of all highlighted vertices are F = {c, e}.
However, we only consider updating the
label of c since e is not adjacent to b.
Since w(b) + w(bc) = 3 + 5 = 8 ≮ 7 = w(c)
we do not update the labels of any vertices.
Since the minimum weight for all vertices in
F is that of c we highlight the edge dc and the
Vertex c.
Don’t write or
This terminates the iterations of the algorithm since our ending vertex place any image
in this area
has been reached.
Dr. D Khalandar Basha
Dijkstra Algorithm
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Dijkstra Algorithm
Step 7: Let u = c.
Then the neighbors of all highlighted vertices
are F = {e}.
However, we do not need to update any labels
since c and e are not adjacent.
Thus we highlight the edge de and the vertex e.
This terminates the iterations of the algorithm since all vertices are now
highlighted.
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Dijkstra Algorithm
Output: The shortest paths from g to all other vertices can be found
highlighted above. For example the shortest path from g to c is g f d c Don’t write or
place any image
and has a total weight 7, as shown by the label of c. in this area
Dr. D Khalandar Basha
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Fleury’s Algorithm
Does the graph below have an Euler Circuit? If so, find one.
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Key points
DFS is an algorithm for traversing a Graph or a Tree.
DFS starts with the root node and explores all the nodes along the depth of the
selected path before backtracking to explore the next path.
DFS makes use of Stack for storing the visited nodes of the graph / tree.
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Example: Consider the below step-by-step DFS traversal of the tree. If the source is
root ( node 0 ), the nodes 2 & 4 along the depth of the tree are explored before the
other nodes in the tree.
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Step 1: Start with the root (source), mark it as visited and push it in the Stack
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Don’t write or
place any image
in this area
Dr. D Khalandar Basha
Key points
Breadth First Search (BFS) is an algorithm for traversing an unweighted Graph or a
Tree.
BFS starts with the root node and explores each adjacent node before exploring
node(s) at the next level.
BFS makes use of Queue for storing the visited nodes of the graph / tree.
Dr. D Khalandar Basha
Example : Consider the below step-by-step BFS traversal of the tree. If the source is
root ( node 0 ), the immediately connected nodes 1 & 2 are considered at the same
level and are explored before the other nodes in the tree
Dr. D Khalandar Basha
Walk
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link] 1023/topics/Topic-20211/subtopics/Subtopic-266111/
[Link]
Don’t write or
[Link]
place any image
tions%3A_An_Introduction_to_Topics_in_Discrete_Mathematics_(Sylvestre)/15%3A_Paths_and_connect
in this area
edness/15.02%3A_Walks_trails_and_paths
Dr. D Khalandar Basha
Data structure organizes the storage in computers so that we can easily access and
change data.
Stacks and Queues are the earliest data structure defined in computer science.
A simple Python list can act as a queue and stack as well.
A queue follows FIFO rule (First In First Out) and used in programming for sorting.
It is common for stacks and queues to be implemented with an array or linked list.
Dr. D Khalandar Basha
A Stack is a data structure that follows the LIFO(Last In First Out) principle.
Dr. D Khalandar Basha
stack = []
A Queue follows the First-in-First-Out (FIFO) principle. It is opened from both the
ends hence we can easily add elements to the back and can remove elements from
the front.
To implement a queue, we need two simple operations:
enqueue - It adds an element to the end of the queue.
dequeue - It removes the element from the beginning of the queue.
Dr. D Khalandar Basha