You are on page 1of 26

Graphs

Mohammed
Fahmi
Kharmah
Table Of Contents
Graph.
Connectivity
Graph search methods.
Topological sort
Graph
In general, a graph is composed of edges E and vertices V
that link the nodes together.
A graph G is often denoted G=(V,E) where V is the set of
vertices and E the set of edges.
Types of graphs from:
Direction:
1.Directed graphs: G=(V,E) where E is composed
of ordered pairs of vertices; i.e. the edges have
direction and point from one vertex to another
{Street networks within cities}.
2. Undirected graphs: G=(V,E) where E is
composed of unordered pairs of vertices;
i.e. the edges are bidirectional{Road networks
between cities} .
Weight:
weighted graphs: each edge (or vertex) of G
is assigned a numerical value, or weight like
length, drive-time or speed limit.
Unweighted graphs: there is no cost distinction between various
edges and vertices.
Simplicity:
Simple graph contains no:
Self-loop: is an edge (x, x) involving only one
vertex.
Multi-edge :An edge (x, y) if it occurs more than
once in the graph.
Non-simple: contains either self-loop
or multi-edge or both of them
Loops:
Cyclic graph: :contain cycles
Acyclic graph : does not contain any cycles.
Trees are connected acyclic undirected graphs.
 Directed acyclic graphs are called DAGs. They arise naturally
in scheduling problems
Data Structures for Graphs
Let us assume the graph G = (V, E) contains n vertices and
m edges.
Adjacency matrix representation
In this representation, each graph of n vertex is represented by
an n x n matrix A, that is, a two-dimensional array A
The vertex are (re)-labeled 1,2,…,n
A[i][j] = 1 if (i,j) is an edge
A[i][j] = 0 if (i,j) is not an edge
Mohammed0 Ahmad1
0 0 0 0 0 0
0 0 0 0 0 0
A =1 1 0 0 1 1 Majd
1 1 0 0 1 1 Sara 2 3
1 1 0 0 0 0
1 1 0 0 0 0
Yara4 Sami5
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
Data Structures for Graphs(cont.)
Adjacency lists representation: consists of a N ×1 array of
pointers, where the ith element points to a linked list of the
edges incident on vertex i.

L[0]: empty Mohammed0 Ahmad1


L[1]: empty
Majd3
L[2]: 0, 1, 4, 5 Sara 2
L[3]: 0, 1, 4, 5
L[4]: 0, 1 Yara4 Sami5

L[5]: 0, 1
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].
Graph Connectivity
An undirected graph is said to be connected if
there is a path between every pair of nodes.
Otherwise, the graph is disconnected
Informally, an undirected graph is connected if it
hangs in one piece

Disconnected Connected
Connected Components
If an undirected graph is not connected, then each
“piece” is called a connected component.
A piece in itself is connected, but if you bring any
other node to it from the graph, it is no longer
connected
If the graph is connected, then the whole graph is one
single connected component
!!Given any undirected graph G,
Is G connected?
If not, find its connected components.
Strongly Connected Components of a Digraph
 Strongly connected:
A directed graph is strongly connected if and only if, for each
pair of vertices v and w, there is a path from v to w.
Graph Traversal Techniques
 The previous connectivity problems, can be solved using graph
traversal(visiting each vertex in the graph) techniques.

 There are two standard graph traversal techniques:


Depth-First Search (DFS)
Breadth-First Search (BFS)

 In both DFS and BFS, the nodes of the undirected graph are visited
in a systematic manner so that every node is visited exactly one.

 Both BFS and DFS give rise to a tree:


When a node x is visited, it is labeled as visited, and it is added
to the tree
If the traversal got to node x from node y, y is viewed as the
parent of x, and x a child of y
Depth-First Search (DFS)
 Search algorithm for both directed and undirected graphs.
 Starting from the source node s, DFS checks the edges from the last
visited node, until reaching a node v whose descendants were all
visited, than back tracks to the previous node and continues the
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.
Illustration of DFS
Stack data structure implementation by increasing order of
their depth 0 1
0
2
1 4
4 9
10 5 7
2
9 11 8
5 6
7 11
6 Graph G
8 10
DFS Tree
Implementation of DFS
Observations:
the last node visited is the first node from which to proceed.
Also, the backtracking proceeds on the basis of "last visited,
first to backtrack too".
This suggests that a stack is the proper data structure to
remember the current node and how to backtrack.
DFS(input: Graph G) {
Stack S; Integer x, t;
while (G has an unvisited node x){
visit(x); push(x,S);
while (S is not empty){
t := peek(S);
if (t has an unvisited neighbor y){
visit(y); push(y,S); }
else
pop(S);
}
}
}
Breadth-First Search (BFS)
 Search algorithm for both directed and undirected graphs.
 Starting from the source node s, BFS computes the minimal distance
from s to any other node v that can be reached from s.
 The algorithm builds a breadth-tree rooted at s with the shortest paths
to nodes that can be reached from s.

 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.
Illustration of BFS
0 1
0
2 4 2
1
9 4
5 9 10
10 5 7

11 8
6 7 8 11
6

BFS Tree Graph G


Implementation of BFS
Observations:
the first node visited in each level is the first node from
which to proceed to visit new nodes.
This suggests that a queue is the proper data structure to
remember the order of the steps.
BFS(input: graph G) {
Queue Q; Integer x, z, y;
while (G has an unvisited node x) {
visit(x); Enqueue(x,Q);
while (Q is not empty){
z := Dequeue(Q);
for all (unvisited neighbor y of z){
visit(y); Enqueue(y,Q);
}
}
}
}
Topological sort
 Definition:
Ordering the vertices of a given directed acyclic graph (DAG), such that if there
is a path from v to u in G, then v appears before u in the ordering.
(Topological sort may result in more than one ordering)
 Theorem    A directed graph has a topological ordering if and only if it is acyclic.
 Proof:
Part 1.  G has a topological ordering if is G acyclic.
Let  G is topological order, Let  G has a cycle (Contradiction).
Because we have topological ordering. We must have  i0, < i, < . . . < ik-1 < i0,
which is clearly impossible, Therefore,  G must be acyclic.
Part 2.  G is acyclic if has a topological ordering.
Let is  G acyclic.
Since  is  G acyclic, must have a vertex with no incoming edges. Let v1 be
such a vertex. If we remove v1 from graph, together with its outgoing edges,
the resulting digraph is still acyclic. Hence resulting digraph also has a vertex
*
 consider the problem of taking a course only after taking its prerequisites.
Topological sort Algorithm
• To find a topological sort is to consider in-degrees of the
vertices.
• The first vertex must have in-degree zero, every DAG must
have at least one vertex with in-degree zero. WHY?????
int topologicalOrderTraversal( ){
int numVisitedVertices = 0;
while(there are more vertices to be visited){
if(there is no vertex with in-degree 0)
break;
else{
select a vertex v that has in-degree 0;
visit v;
numVisitedVertices++;
delete v and all its emanating edges;
}
}

return numVisitedVertices;
}
PS.Topological sort is not unique.
Example
Next Example
1 2 3 0 2
A B C D E

F G H I J
1 0 2 2 0
D G A B F H J E I
C
Time Complexity Summary
Algorithm Time complexity

Breadth-First Search (BFS) O(V + E)

Depth-First Search(DFS) O(V + E)

Topological Sort O(V+E)

You might also like