Graph Structures and Algorithms
• Graphs are more general than trees
– Every tree is a graph, but
– not every graph is a tree
• You will learn about
–
–
–
–
–
–
Definition & representation of various types of graphs
Topological sort and shortest path algorithms
Graphs applied to network flow problems
Minimum spanning tree algorithms
Search algorithms and their applications
An introduction to NP Completeness
Definitions
• A graph G is a set of vertices V and edges E
– an edge (or arc) is a pair (u,v) where u,v V
– If the edge is an ordered pair then the graph is a directed graph
(digraph)
– In some problems an edge may be weighted
• Other terms
– A path is a sequence of vertices connected by edges; in a
simple path all edges are unique
– Although a vertex may have an edge connecting to itself, we
will not consider such graph edges
– A cycle is a path that starts and ends at the same vertex; a
graph with no cycles is called acyclic
– A graph is connected if there is a path for every vertex to every
other vertex
Graph Representation - 1
• Representing a graph by an adjacency matrix of
0’s and 1’s means there is a 1 if there is an edge
• If the graph is
undirected then the
matrix will be
symmetric
0
1
1
1
0
0
0
0
0
0
1
1
0
0
0
0
0
0
0
1
0
Graph
Representation
-2
• Using an
adjacency list
• If there are
few edges,
then an
adjacency list
may be more
efficient
Group Work
• Give the adjacency matrix
and adjacency list
representations for this
graph
Group Work
• Suppose you have a digraph with 100 vertices
and 1000 edges with numeric values as weights
– How many words of memory would an adjacency
matrix require
– How many words of memory would an adjacency list
require
• For a graph with N vertices, approximately how
many edges would be present for the adjacency
list and adjacency matrix to take the same
memory space?
The Topological Sort Problem
• A topological sort of a directed, acyclic graph is an
ordering of vertices such that if there is a path from vi to
vj then vj appears after vi in the ordered vertices
• Here is a prerequisite
course structure, any
ordering of courses
that does not violate a
prerequisite
requirement would
satisfy the
topological sort
problem
An Example of a Topological Sort
• Given the
acyclic digraph
• Two topological
sorts are
v1, v2, v5, v4, v3, v7, v6
v1, v2, v5, v4, v7, v3, v6
• Indegree and Outdegree
• The indegree is the number of incoming edges
• The outdegree is the number of outgoing edges
• The basic algorithm is to select a vertex with
indegree zero, eliminate it and its outgoing
edges, then select the next zero degree vertex
Pseudocode Algorithm
/*
/*
/*
/*
1*/ void
2*/ Topsort( Graph G )
3*/ {
4*/
Vertex V, W;
/* 5*/
/* 6*/
/* 7*/
/* 8*/
/* 9*/
/*10*/
/*11*/
/*12*/
/*13*/
/*14*/
/*15*/
/*16*/
/*17*/ }
for( int Counter = 1; Counter <= Num_Vertex; Counter++ )
{
V = Find_New_Vertex_Of_Indegree_Zero( );
if( V == Not_A_Vertex )
{
Error( "Graph has a cycle" );
return;
}
Top_Num[ V ] = Counter;
for Each W Adjacent To V
Indegree[ W ]--;
}
Applying the
Topological
Sort Algorithm
An Analysis and Possible Improvements
• Complexity analysis
– The indegree array has size V
– V scans of an array of size V results in O(V2)
• An improvement
– Keep all zero degree vertices in a separate “box”
– Select the next node from the box, decrease the degree
of adjacent vertices and put any that reach zero into
the box; we can use a queue for the “box”
– Using an adjacency list structure, the complexity of
this revised approach is O(|E| + |V|)
– The peudocode is on the next slide
/*
/*
/*
/*
/*
/*
1*/ void
2*/ Topsort( Graph G )
3*/ {
4*/
unsigned int Counter = 1;
5*/
Vertex V, W;
6*/
Queue<Vertex> Q( Num_Vertex );
/* 7*/
/* 8*/
/* 9*/
for Each Vertex V
if( Indegree[ V ] == 0 )
Q.Enqueue( V );
/*10*/
/*11*/
/*12*/
/*13*/
while( !Q.Is_Empty( ) )
{
V = Q.Dequeue( );
Top_Num[ V ] = Counter++;
/*14*/
/*15*/
/*16*/
/*17*/
/*18*/
/*19*/
/*20*/ }
for Each W Adjacent To V
if( --Indegree[ W ] == 0 )
Q.Enqueue( W );
}
if( Counter <= Num_Vertex )
Error( "Graph has a cycle" );
Revised
Pseudocode
Algorithm
Group Work
• Using the revised
algorithm, find the
topological sort for
this graph (ignore
the weights)
© Copyright 2026 Paperzz