Java TOPIC 24 – INTRO TO GRAPHS LESSON NOTE GRAPHS Graphs
are structures that are often used in computer science in order to solve
advanced programs. It is sometimes
hard to tell if a Graph should be used in a solution. Graphs are often useful to discover
pathways from one location to another.
WHAT IS A GRAPH? A
graph is a structure that consists of vertices
and edges between some (or all) or
the vertices. An edge generally
represents a path of travel between the two vertices. Below
are some visual examples: Example 1 Example 2 This
graph has 7 vertices (0 to 6) and 11 edges (01, 02, 03, 12, 15, 16, 24, 34,
35, 46, 56).
Notice that the edges have arrow heads. This means that this is a directed
graph. Directed
graphs could be used to demonstrate that going from vertex 0 to 3 is
possible, but not vice versa (unless we first travelled to 5 and then 1 and
then 0). Example 3 This
graph includes a weight with each
edge. These weights can represent the
cost of travelling along that edge.
The cost could refer to many things including time required, or
distance or perhaps financial cost. In
the example below, we can ask the problem "What is the lowest-cost way
to travel from Vertex e to Vertex a?
The answer is to travel to f, then to c then to a
for a cost of 9 + 2 + 9 = 20.
All other possible paths are less cost efficient. Example 4 The
graph below contains no cycles. This
is called a tree. Trees are often considered a separate type
of data structure from graphs because they can be used for different types of
problems. However, really they are
just a specialized graph. Example 5 The
graphs below show the difference between a connected graph and a disconnected
graph. As you can see, in
disconnected graphs, it is impossible to travel from one location to all
others. TERMINOLOGY Adjacent
– Two vertices are adjacent to one
another if they are connected by an edge. Path
– A path is a list of vertices
such that each vertex in the list is adjacent to the vertex before and after
it. Degree
of a vertex – The degree of a vertex
is number of edges that the vertex has connecting it to other vertices. Vertices with a degree of 1 are can only be
at the end of paths as they are only adjacent to one vertex. APPLICATIONS Graphs
can be used in many different fields and areas. Most of the time, they are used to find or
study paths between locations or items.
Here are a few fields where graphs have been used:
TYPES OF PROBLEMS Most
problems that involve graphs involve finding or studying paths between two
vertices. We will now consider a few
such problems. TYPE 1 – FINDING ALL
PATHS First,
we will look at finding all the possible paths between two vertices. We only consider paths that do not include
cycles and never visit a vertex more than once. Such paths are called simple paths.
TYPE 2 – SIMPLE PATH
THROUGH ALL VERTICES We
will now look at problems that involve trying to find a path that will visit
all vertices in a graph. That is, to
find a path that will not visit any nodes more than once.
TYPE 3 – LOWEST COST
PATH A
common task related to graph problems is to find a path between two
vertices. However, it is often
required that we find the lowest cost path between them.
TYPE 4 – SHORTEST PATH
TO ALL VERTICES This
type of problem requires that we visit all vertices all while finding the
lowest cost path. It may require that
we visit a node more than once as well.
TYPE 4B – TRAVELLING
SALESMAN PROBLEM The
Traveling Salesman Problem (or
just TSP) is a famous computer
science problem that involves the finding of the shortest path through all
vertices but that also starts and ends at the same vertex. It
has many real-world applications. It's name is a reference to the
challenge that sales people have in mapping out their path when visiting
different cities (or different locations).
This
problem is particularly famous because it is quite easy to understand but finding
the best solution for a problem is quite difficult. Certainly, we can just calculate every
possible path and find the one with the lowest cost. However, the number of paths is so large that
computers aren't close to being fast enough to solve large problems. The
number of possible paths for a fully connected graph with n vertices is n-factorial.
So, a graph with 10 vertices has 10-factorial paths which is 3,628,800. That's right, a graph with only 10 vertices
has over 3.6 million possible paths that start and end at the same location. A
graph with 20 vertices has 20 factorial possible paths which is 2,432,902,008,176,640,000
or better known as about 2.4 quintillion paths. Don't know what a quintillion is? It's 1000 quadrillions or 1 million trillion. You get the point, this number is crazy! And it's for only 20 vertices! REAL WORD EXAMPLE The
following TSP problem involves finding the shortest path that goes through
all American state capitals (shown below).
There are 48 capitals, so 48 vertices.
That means there are 48-factorial possible paths. 48! =12,413,915,592,536,072,670,862,289,047,373,375,038,521,486,354,677,760,000,000,000 Or about 12.4 novemdecillion The
solution found looked like this: That's
it for now. If time permits, we will
revisit the Travelling Salesperson Problem again. |
|||||||||||||||
|