Академический Документы
Профессиональный Документы
Культура Документы
Graph Algorithm
Introduction
The objective of this article is to provide a basic introduction about graphs and the commonly
used algorithms used for traversing the graph, BFS and DFS. Breadth First Search (BFS) and
Depth First Search (DFS) are the two popular algorithms. This chapter will help you to get
some basic understanding about what graphs are, how they are represented, graph traversals
using BFS and DFS and time/space complexity of each algorithm.
Graph Algorithms: We are now beginning a major new section of the course. We will be
discussing algorithms for both directed and undirected graphs. Intuitively, a graph is a
collection of vertices or nodes, connected by a collection of edges. Graphs are extremely
important because they are a very flexible mathematical model for many application
problems. Basically, any time you have a set of objects, and there is some “connection” or
“relationship” or “interaction” between pairs of objects, a graph is a good way to model this.
Examples of graphs in application include communication and transportation networks, VLSI
and other sorts of logic circuits, surface meshes used for shape description in computer-aided
design and geographic information systems ,precedence constraints in scheduling systems.
The list of application is almost too long to even consider enumerating it.
Most of the problems in computational graph theory that we will consider arise because they
are of importance to one or more of these application areas. Furthermore, many of these
problems form the basic building blocks from which more complex algorithms are then built.
Definition of Graph
A graph G = (V, E) consists of a (finite) set denoted by V, or by V(G) if one wishes to make
clear which graph is under consideration, and a collection of E, or E(G), of unordered pairs
{u, v} of distinct elements from V. Each element of V is called a vertex or a point or a node,
and each element of E is called an edge or a line or a link.
Graphs are good in modeling real world problems like representing cities which are
connected by roads and finding the paths between cities, modeling air traffic controller
system, etc. These kinds of problems are hard to represent using simple tree structures. The
following example shows a very simple graph:
In the above graph, A,B,C,D,E,F are called nodes and the connecting lines between these
nodes are called edges. The edges can be directed edges which are shown by arrows; they
can also be weighted edges in which some numbers are assigned to them. Hence, a graph
can be a directed/undirected and weighted/un-weighted graph. In this article, we will
discuss undirected and un-weighted graphs.
1. Nodes
A graph is a set of nodes and a set of edges that connect the nodes. Graphs are used to model
situations where each of the nodes in a graph must be visited, sometimes in a particular order,
and the goal is to find the most efficient way to “traverse” the graph. The elements of a graph
are called nodes, and the elements that are below a particular node are called the node’s
children. it is represented by Circle.
2. Edges
Edges represent the connection between nodes. There are two ways to represent edges.
Adjacency Matrix
It is a two dimensional array with Boolean flags. As an example, we can represent the edges
for the above graph using the following adjacency matrix.
In the given graph, A is connected with B, C and D nodes, so adjacency matrix will have 1s
in the ‘A’ row for the ‘B’, ‘C’ and ‘D’ column.
The advantages of representing the edges using adjacency matrix are:
Simplicity in implementation as you need a 2-dimensional array
Creating edges/removing edges is also easy as you need to update the Booleans
A D
B C
As stated before, in DFS, nodes are visited by going through the depth of the tree from the
starting node. If we do the depth first traversal of the above graph and print the visited
node, it will be “E F B C D A”. DFS visits the root node and then its children nodes until
it reaches the end node, i.e. E and F nodes, then moves up to the parent nodes.
Algorithmic Steps
Step 1: Push the root node in the Stack.
Step 2: Peek the node of the stack.
Step 3: If the node has unvisited child nodes, get the unvisited child node, mark it as
traversed and push it on stack.
Step 4: If the node does not have any unvisited child nodes, pop the node from the stack
Step 5: Loop step 2 until stack is empty.
.
Ao-------------oB
D
B o-------------oC
Exercise
This graph have sixty spanning tree try it by yourself
Now suppose the edges of the graph have weights or lengths. The weight of a tree is just the
sum of weights of its edges. Obviously, different trees have different lengths.
Dijkstra's algorithm, named after its discoverer, Dutch computer scientist Edsger Dijkstra, is
a greedy algorithm that solves the single-source shortest path problem for a directed
graph with non negative edge weights. For example, if the vertices (nodes) of the graph
represent cities and edge weights represent driving distances between pairs of cities
connected by a direct road, Dijkstra's algorithm can be used to find the shortest route
between two cities. Also, this algorithm can be used for shortest path to destination in
traffic network.
Using the Code
We are in A node and the problem is we must go other nodes with minimum cost. L[,] is our
distances between pairs of nodes array.
Collapse | Copy Code
int[,] L ={
{-1, 5, -1, -1, -1, 3, -1, -1},
{ 5, -1, 2, -1, -1, -1, 3, -1},
{-1, 2, -1, 6, -1, -1, -1, 10},
{-1, -1, 6, -1, 3, -1, -1, -1},
{-1, -1, -1, 3, -1, 8, -1, 5},
{ 3, -1, -1, -1, 8, -1, 7, -1},
{-1, 3, -1, -1, -1, 7, -1, 2},
{-1, -1, 10, -1, 5, -1, 2, -1}
D[] shows the cost array. We will write the shortest cost in D array. C[] shows our nodes.
Pseudocode
Collapse | Copy Code
function Dijkstra(L[1..n, 1..n]) : array [2..n]
array D[2..n]
set C
C <- {2, 3, 4, 5, 6, …, n}
for i <- 2 to n
D[i] <- L[1,i]
repeat n - 2 times
v <- C // minimum D[v] extract to C
v <- C - {v}
for each w in C do
D[w] <- min(D[w], D[v] + L[v,w])
return D
C[]-> -1, 1, 2, 3, 4, 5, 6, 7
C[]-> -1,-1,-1,-1,-1,-1,-1,-1