You are on page 1of 28

Graphs

1
Introduction to Graphs
 Representing a problem as a graph can make a problem
much simpler. It can provide the appropriate tools for
solving the problem
 Graphs theory provides a set of techniques for analysing
Complex Networks. In transport network, For instance
nodes may represent cities, while edges may represent
airline flight routes between cities
 Applying network theory to a system means using a
graph-theoretic representation

2
Introduction to Graphs
 In graph-like problems, these components have natural
correspondences to problem elements
 Entities are nodes and interactions between entities
are edges
 Most complex systems are graph-like
 Therefore, graph theory concerns the study of networks
based on a mathematical abstraction of the form of a
graph

3
Transportation Networks

4
Basic Graph concepts
• A graph is a non-linear data structure also called a
network
• A graph consists of Vertices (nodes) and a set of
lines connecting pairs of vertices known as Edges
connecting the pairs of vertices (G=(V,E))
• Unlike a tree or binary tree:
– a graph does not have a root – no primary entry point.
– Any node can be connected to any other node by an
edge
– Can have any number of edges and nodes
5
Basic Graph concepts

 Path – a sequence of vertices in which each vertex is


adjacent to the next one. In the figure below, IGFE is a path
 Cycle – a path consisting of at least three vertices that
starts and ends with the same vertex.
 Two vertices are said to be adjacent to one another if they
are connected by a single edge. Adjacent vertices are also
said to be neighbors. In figure 1, vertices H and G are
adjacent

6
Basic Graph concepts

I J

D
F E

B C

7
Basic Graph concepts

Two vertices are said to be connected if there is a path
between them.

A graph is said to be connected if there is a path from any
vertex to any other vertex.

A directed graph is strongly connected if there is a path from
each vertex to every other vertex.

A directed graph is weekly connected if at least two vertices
are not connected.
Basic Graph concepts
Directed Graphs
• In a directed graph also known as a digraph each
edge has a specific direction denoted by arrows.
• Edges with direction sometimes are called arcs
 Used to model situations in which one can go in only one
direction along an edge, such as a one-way street
 The allowed direction is typically shown with an arrow-
head at the end of the edge
 A directed acyclic graph is a directed graph without
cycles, i.e. you can't get back to the vertex where you
started by following edges in their defined direction.
9
Directed graphs
Basic Graph concepts
Non-directed Graphs
 The edges for these graphs do not have a direction. One can
go either way on them
 In the Figure below, one can go from B to E or E to B on the
same edge

11
Basic Graph concepts
Weighted Graphs
 is a graph for which each edge has an associated weight,
usually given by a weight function w: E  R.
 Such a weight can be used to represent some property of
the connection e.g. distance, cost, time taken to travel on
the route etc.

1.2 2
1 2 3 1 2 3
.2
.5 1.5 5 3
.3 1
4 5 6 4 5 6

.5
12
Graph Implementation
 Each node will be represented with a structure that is
appropriate to store the information associated with a
node E.g.
 A record /Structure (in C)
 An object (object oriented languages)
 A hash (in perl)
 Two common methods:
Array (adjacency matrix)
Linked list (adjacency list)
13
Adjacency Matrix
 We need to represent both the vertices and the edges
 Vertices can be represented using an array.
 In this sense, nodes have to be numbered from node 0, to node
n-1 (where n is the number of nodes)
 Node 0 is stored at position 0 in the array, node 1 at position 1,
until node n-1 at position n
 The adjacency matrix is a two dimensional array in which the
elements indicate whether an edge is present between two
vertices
 If a graph has n vertices, then the adjacency matrix is nn array.

14
Adjacency Matrix
 Entry i,j j=0…n-1, i=0…n-1 is 1 if there is an edge going from node i to
node j.
 The entry is zero otherwise
 For a non-directed graph, entry i,j=entry j,i
 Entry i,i can be set as 0 or 1 as is convenient.
  0 1 2 3
2
0 0 0 1 1 0
3
1 1 0 1 1

1 2 1 1 0 1

3 0 1 1 0
Fig 3: non-directed
graph Fig 4: Adjacency matrix for figure 3

15
Pros and Cons of Adjacency Matrices

 Pros:
Simple to implement
Easy and fast to tell if a pair (i,j) is an edge:
simply check if A[i][j] is 1 or 0
 Cons:
No matter how few edges the graph has, the
matrix takes O(n2) in memory

CS 103 16
Adjacency List
 The other way to represent edges is using an adjacency list
 An adjacency list is actually an array of lists
 The adjacency list is the preferred way to represent sparse
graphs
 An adjacency list is defined using a one dimensional array.
 Each array position corresponds to a node of the graph.
 The entry for each position is a list, containing all vertices
adjacent to that node.

17
Pros and Cons of Adjacency Lists

 Pros:
Saves on space (memory): the representation
takes as many memory words as there are
nodes and edge.
 Cons:
It can take up to O(n) time to determine if a pair
of nodes (i,j) is an edge: one would have to
search the linked list L[i], which takes time
proportional to the length of L[i].

CS 103 18
Graph Traversal
 One of the most fundamental operations to perform on a
graph is finding which vertices can be reached from a
specified vertex

You must somehow ensure that you process the data in
each vertex only once.

Two common graph traversals
 Depth-first traversal – You process a vertex’s
descendants before you move to an adjacent vertex.
 Breadth-first traversal – You process all adjacent
vertices of a vertex before going to the next level.
Depth-First Search
 DFS follows the following rules:
1. Select an unvisited node x, visit it, and treat as the
current node
2. Find an unvisited neighbor of the current node, visit it,
and make it the new current node;
3. If the current node has no unvisited neighbors,
backtrack to the its parent, and make that parent the
new current node;
4. Repeat steps 3 and 4 until no more nodes can be
visited.
5. If there are still unvisited nodes, repeat from step 1.

CS 103 20
Depth-First Search Algorithm
 Uses a stack to remember where it should proceed with the search
 This algorithm searches deeper into the graph wherever possible.
 Given the starting node s, and that pop returns null if the stack is empty.
c is the current node
1) c:=s
2) Mark c as visited
3) If there is a node x adjacent to c not yet
visited
4) Push c into the stack
5) c:=x
6) mark c as visited
7) Else
8) c=pop item on top of stack
21
9) Repeat from 3 while c<>null
Breadth-First Search
 BFS follows the following rules:
1. Select an unvisited node x, visit it, have it be the
root in a BFS tree being formed. Its level is called
the current level.
2. From each node z in the current level, in the order
in which the level nodes were visited, visit all the
unvisited neighbors of z. The newly visited nodes
from this level form a new level that becomes the
next current level.
3. Repeat step 2 until no more nodes can be visited.
4. If there are still unvisited nodes, repeat from Step 1.

CS 103 22
Breath-First Search Algorithm
 Uses a FIFO queue to remember where it should proceed with the
search
 The algorithm first visits the nodes that are closest to the start node
 Its proceeds as follows given the starting node s,
 Assume that dequeue returns null if the queue is empty and that the
current node is v
1) v:=s
2) Mark v as visited
3) For all x such that x is a child of v not yet visited
4) mark x as visited
5) add x to the back of the queue
6) v:= first node in the queue (dequeue)
7) Repeat from 3 while v<>null

23
Java codes for Bfs

import java.io.*;
import java.util.*;
class Graph
{
private int numVertices;
private LinkedList<Integer> adjLists[];
private boolean visited[];
Graph(int v)
{
numVertices = v;
visited = new boolean[numVertices];
adjLists = new LinkedList[numVertices];
for (int i=0; i i = adjLists[currVertex].listIterator();
while (i.hasNext())
{
int adjVertex = i.next();
if (!visited[adjVertex])
{
visited[adjVertex] = true;
queue.add(adjVertex);
}
}
}
}

24
cont
public static void main(String args[])
{
Graph g = new Graph(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);
System.out.println("Following is Breadth First Traversal "+
"(starting from vertex 2)");
g.BFS(2);
}
•}

25
Applications of Graphs
 Computer science- used to represent networks of
communication data organization, computational devices
 Physics and chemistry – used to study molecules in
chemistry and physics
 Mathematics- used in geometry and certain parts of
topology such as knot theory
 Biology- graphs theory is used in biology and
conservation efforts where vertex can represent regions
where certain species exist and edges rep migration
Con….

 Sociology –used as a way to measure


actors prestige or to explore diffusion
mechanism, notably through the use of
social network analysis software
 Google maps- the roads represent edges
while the junctions represents the nodes

27
The end

THANK YOU!

28

You might also like