Professional Documents
Culture Documents
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[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.
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.
11 8
6 7 8 11
6
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