2. Graph theory
• A graph is used in mathematics and computer science
to represent pairwise relations between objects.
• It is represented as a collection of nodes and edges.
The edges connect the nodes.
• Each node or/and edge can have certain values or
names assigned to it.
• Two nodes are called neighbors if they are connected
with an edge.
• Two edges are connected if they have a common node.
• simple graph:
3. Example of a graph:
In this example the nodes are the cities in Macedonia and the
edges are the roads between a pair of cities.
4. Definition of the Eulerian path
problem
• Urban definition:
Given a drawing on a piece of paper, try to cross all the lines
with a pen such that the pen doesn’t leave the paper and you
mustn’t cross a line more than once.
• Professional definition:
Given a graph G, find a sequence of connected edges S such
that every edge of G is found in S.
• Try solving:
5. The Algorithm
• Step 1: we first have to input the graph, we will call the graph G.
• Step 2: next we have to check if G is valid. If G is not valid, no solution exists and stop the algorithm.
– How to check if G is valid:
• First we have to determine the number of neighboring nodes to every node of G, we will keep
this numbers in the sequence X.
• Than we count how many odd numbers are there are in X, and we will call this number N.
• If N = 0 or N = 2 the graph is valid, else the graph is not valid.
• Step 3: next we have to find a starting node, we will name this node W.
– if N = 0 than set W to a random node
– If N = 2 than set W to a node with an odd number of neighbors
• Step 4: define a stack S and push W onto S, also define a sequence of nodes P, P will contain the eulerian
path solution to G.
• Step 5: while S is nonempty repeat the following steps:
– if W has at least one neighbor
• Set O to the nearest node to W
• Disconnect O from W
• Set W to O
• Go to step 5
– Else add W to P, set W to the top of S, Pop the first element from S
• Step 5: print P and stop the algorithm.
e.g. graph