0% found this document useful (0 votes)
24 views120 pages

Data Structure - Algorithm - Algorithm Analysis

The document discusses data structures and algorithms. It provides definitions for key terms like data structure, algorithm, algorithm analysis, and big-O notation. It then discusses the selection sort algorithm, including its advantages and disadvantages. It analyzes selection sort as having O(n) time complexity for swaps. It introduces the ordered (unsorted) collection abstract data type and some of its operations like creation, length, indexing, addition, and removal. It also discusses concepts like abstraction, physical vs logical size, and information hiding. Finally, it provides examples of linked list implementations for operations like insertion, removal, and the copy constructor.

Uploaded by

Mohamad A Sallal
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views120 pages

Data Structure - Algorithm - Algorithm Analysis

The document discusses data structures and algorithms. It provides definitions for key terms like data structure, algorithm, algorithm analysis, and big-O notation. It then discusses the selection sort algorithm, including its advantages and disadvantages. It analyzes selection sort as having O(n) time complexity for swaps. It introduces the ordered (unsorted) collection abstract data type and some of its operations like creation, length, indexing, addition, and removal. It also discusses concepts like abstraction, physical vs logical size, and information hiding. Finally, it provides examples of linked list implementations for operations like insertion, removal, and the copy constructor.

Uploaded by

Mohamad A Sallal
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 120

• Data Structure: is the way in which the data of

the program are stored.


• Algorithm: is a well defined sequence of
computational steps to solve a problem.
• Algorithm Analysis: is the number of steps or
instructions and memory locations needed to
perform a certain problem for any input of a
particular size.
• A big-O Analysis: is a technique for estimating
the time and space requirements of an
algorithm in terms of order of magnitude.
Selection Sort
void SelectionSort( apvector < element > &list, int n )
{
int MinPosition;
element temp;
for ( int k=0 ; k < n-1 ; ++k )
{
MinPosition = k;
for ( int j = k+1 ; j < n ; ++j )
if ( list[ j ] < list[ MinPosition ] )
MinPosition = j;
if ( MinPosition != k )
{
temp = list[ MinPosition ];
list[ MinPosition ] = list[ k ];
list[ k ] = temp;
}
}
}
Selection Sort
• Advantages: It reduces the number of data
interchanges.
• Disadvantages: it does not allow an effective &
automatic loop exit if the vector becomes
ordered during an early pass

O (n) (swaps),
The Ordered (Unsorted) Collection Abstract Data
Type
Abstract data type (ADT): a form of abstraction
that arises from the use of defined types. An ADT
consists of a class of objects, a defined set of
properties of those objects, and a set of operation
for processing the objects.

Abstraction: the description of data structures at a


conceptual level, apart from their implementation
using a particular technique & language.
• Physical size: the number of memory units (locations)
available for storing data items in a data structure.

• Logical size: the number of data items actually


available in a data structure of a given time.

• The importance of distinguishing between physical


and logical size is to prevent causing errors when
users attempt to access data locations in that portion
of a vector where no data values have yet been
stored.
• Users can keep track of the data within a physical
vector by maintaining a separate integer variable
(length) as a counter of the number of data
elements currently stored in the vector. Any indexed
reference to a data element in a vector, should use
an index that satisfies the condition 0 <= index <
length.
 
• Ordered (unsorted) collection: a data structure
that supports indexing for retrieval or change of
data items, the detection of the logical size of
the structure, & addition or removal of data
items from the logical end of the structure.
• The ordered (unsorted) collection ADT is like a
vector in that it provides indexed access to data
locations, and unlike a vector in providing indexed
access just to those data locations where
elements have been stored.

• The ordered (unsorted) collection class provides


its own logical size is a property & provides a set
of operations for adding or removing data
elements.
Evaluating the Implementation
• ADT use rule: algorithms that use an ADT should
access or modify the ADT only through the
operations provided in the ADT definition.
• ADT implementation rule: An implementation
of an ADT must provide an interface that is
entirely consistent with the operations specified
in the ADT’s formal definition.
• Information hiding: the process of suppressing
(hiding) the implementation details of a function
or data structure from higher-level logic, so as to
simplify its use in programming.
• Creation operation
• Function: To create an ordered (unsorted)
collection.
• Preconditions: The OC is in an unpredicate state.
• Postconditions: An OC of length 0 is created.

OrderedCollection<E>::OrderedCollection( const OrderedCollection


&OC )
: data( MAX_VECTOR_SIZE )
{
for ( int j=0 ; j < OC.clength ; ++j )
data[ j ] = OC.data[ j ];
clength = OC.clength;
}
• Length Operation
• Function: To return the length of the OC.
• Preconditions: The OC is initialized.
• Postconditions: The number of data elements
in the OC is returned.

template< class E >


int OrderedCollection<E>::length()
{
return clength;
}
• Indexing Operation
• Function: To return the data element associated by that
index.
• Preconditions: - The OC is initialized.
The index parameter is an integer value in the range 0 ≤ index <
length of OC.
• Postconditions: The location of the data element as specified
by index, which can be used either to reference or to store a
value, is returned.

template< class E >


E &OrderedCollection<E>::operator[]( int index )
{
assert ( ( index >= 0 ) && ( index < clength ) );
return data[ index ];
}
• AddLast Operation
• Function: To add a new data element at the end of the
collection.
• Preconditions: - The OC is initialized.
–The parameter is an object of type E.
–There is memory available to store the new item in the collection.
• Postconditions: The length of the OC is incremented by 1, &
the parameter object is placed at the end of the OC.

template< class E >


void OrderedCollection<E>::AddLast( E e )
{
assert( clength< MAX_VECTOR_SIZE );
data[ clength ] = e;
++clength;
}
• RemoveLast Operation

• Function: To remove a data element from the end of the OC.


• Preconditions: The OC is initialized & the length of the OC > 0.
• Postconditions: The length of the OC is decreased by 1, &
what had been the last data element in the OC is returned.

template< class E >


E OrderedCollection<E>::RemoveLast()
{
assert( clength > 0 );
--clength;
E removeItem = data[ clength ];
return removeItem;
}
• AddFirst Operation
• Function: To add a new data element to the beginning of an OC.
• Preconditions: - The OC is initialized.
– The parameter is an object of type E.
– There is memory available to store the new item in the OC.
• Postconditions: - The length of the OC is incremented by 1.
– The data elements in the OC are shifted up by one index position.
– The parameter element is placed in the first position in the OC.

void OrderedCollection<E>::AddFirst( E e )
{
assert( clength < MAX_VECTOR_SIZE );
for ( int i=clength ; i > 0 ; --i )
data[ i ] = data[ i – 1 ];
data[0] = e;
++clength;
}
• RemoveFirst Operation
• Function: To remove a data element from the beginning
of an OC.
• Preconditions: The OC is initialized, and the length of the
OC > 0.
• Postconditions: - The length of the OC is decreased by 1.
– The data elements in the OC that come after the first
element are shifted down by one index position.
– What had been the first element in the OC is
returned.
• Assignment Operation
• Function: To assign the contents of the source object
into the target object.
• Preconditions: - The target is an OC, initialized.
– The source is an OC, initialized.
• Postconditions: The contents of the source object are
copied into the target object, and the target’s length is
set to the source’s length.
• Remove Operation
• Function: To remove a data element from the OC.
• Preconditions: - The OC is initialized.
– The parameter is the data element to be removed.
– The parameter must equal an element currently in the
list.
– The length of the OC > 0.
• Postconditions: - The length of the OC is decreased by 1.
– The data elements in the OC that come after the
parameter element are shifted down by one index
position.
• 8) Add Operation
• Function: To add a new data element in its proper position
in SC.
• Preconditions: - The SC object is initialized.
– The parameter is an object of type E.
– There is memory to store the new element in the SC.
• Postconditions: - The length of the SC is incremented by 1.
– The parameter object is placed in its proper position on
the SC.
Implementation:
template< class E >
void SortedCollection<E>::Add( E e )
{
int place = 0;
assert( clength < MAX_VECTOR_SIZE );
if ( ( clength == 0 ) || ( e > data[clength - 1] ) )
data[ clength ] = e;
else
{
while ( e > data[ place ] )
++place;
for ( int index=clength ; index > place ; --index )
data[ index ] = data[ index - 1 ];
data[ place ] = e;
}
++clength
}
The need for linked list
A central motivation behind the linked list data
structure is to eliminate the data movement
associated with insertion into and deletions from.
Linked list uses only enough dynamic memory to
store the data values inserted by the user.
insert

void LinkedList<E>::insert( const E &item )


{
node *newnode = getNode( item );//create new node

if ( empty() || (myFirst == myCurrent)


myFirst = newnode
else
myPrevious->next = newnode;

newnode->next = myCurrent;
myCurrent = newnode;

++mySize;
}
getNode
template< class E >
LinkedList<E>::node*LinkedList<E>::getNode(const E &item)
{
 node *newnode = new node;

assert ( newnode != 0 );

newnode->data = item ;

newnode->next = 0;
return newnode;
}
remove
E LinkedList<E>::remove(){
assert( myCurrent != 0 );
E data = myCurrent->data

node *garbage = myCurrent;


if ( myFirst == myCurrent )
myFirst = myCurrent->next;

else
myPrevious->next = myCurrent->next;
myCurrent = myCurrent->next;

delete garbage;

--mySize;
return data;
}
The copy constructor for a linked list
template< class E >
LinkedList<E>::LinkedList( const LinkedList<E> &list )
:myFirst(0), myCurrent(0), myPrevious(0), mySize(0)
{
node * probe = list.myFirst;
while ( probe != 0 )
{
insert( probe->data );
 
next();
 
probe = probe->next;
}
}
• Adding Data to the List Doubly Linked List
void LinkedList<E>::insert( int position ,const E &item )
{
node * newNode, * following, * preceding;
assert( position >= 0 || position <= mySize );
if( position == 0 )
{ if( mySize == 0 )
following = NULL;
else
{
set_position( 0 );
following = myCurrent; }
preceding = NULL; }
else
{ set_position( position - 1 );
preceding = myCurrent;
following = preceding->next; }
newNode = new Node( item, preceding, following );

if( newNode == NULL )


return overflow;
if( preceding != NULL )
preceding->next = newNode;
if( following != NULL )
following->back = newNode;
myCurrent = newNode;
Current_Position = position;
MySize++;
return success;
}
Linked list implementation of a stack ADT using inheritance

template <class E>


class apstack : protected linkedlist<E>
{
public:
apstack();
void push(const E &item);
void pop(E &item);
linkedList<E>::first;
linkedList<E>::insert;
linkedList<E>::remove;
};
Infix, prefix, and postfix notation:
Infix notation: the arithmetic operator appears
between the two operands to which it is being
applied.
Infix notation may require parenthesis to specify a
desired order of operations. Example: A / (B + C).
Postfix notation: The operator is placed directly after
the two operands to which it applies.
Converting from infix to postfix:

• Completely parenthesize the infix expression


to specify the order of all operations.

• Move each operator to the space held by its


corresponding right parenthesis.

• Remove all parentheses.


1. A stream of characters containing the infix
expression & terminated by the special delimiter #.
2. A stack named opstack which may contain:
a. Arithmetic operators : + , - , * , /
b. The left parenthesis ( , the right parenthesis is
processed by the algorithm but never stored in
the stack.
c. The special delimiter #.

3. A string named postfix containing the final postfix


expression.
• The description of the algorithm
1. Define a function infixpriority,
2. Define another function stackpriority
3. Initialize opstack by pushing #.
4. Read the next character ch from the infix expression.
5. Test ch and:
 If ch is an operand, append it to the postfix string.
 If ch is a right parenthesis, then pop entries from stack and append
them to the postfix string until a left parenthesis is popped.
 If ch is a #, pop all entries that remain on the stack and append them
to the postfix string.
 Otherwise, pop from the stack and append to the postfix string
operators whose stack priority is greater than or equal to the infix
priority of ch. Stop these series of popping operations when you
reach a stack element where stack priority is less than the infix
priority of ch. Then push ch.
6. Repeat step 4 & 5 until ch is the delimiter #.
void InfixToPostfix( apstring {
&postfix ) while ( ! opstack.isEmpty() )
{ char item , ch; { opstack.pop( item );
apstack< char > opstack; postfix = postfix + item; }
opstack.push('#'); }
do else
{ cin.get(ch); { opstack.pop( item );
if ( ( 'A' <= ch ) && ( ch <= 'Z' ) ) while ( stackpriority( item )
postfix = postfix + ch; >= infixPriority(ch) )
else if ( ch == ')' ) { postfix = postfix + item;
{ opstack.pop( item ); }
opstack.pop( item );
while ( item != '(' ) opstack.push( item );
{ postfix = postfix + item; opstack.push( ch );} }
opstack.pop( item ); }} while ( ch != '#' );
}
else if ( ch == '#' )
Enqueue Operation (Add) inheritance

void apqueue<E>::enqueue( const E &item )


{
myPrevious = myLast;
myCurrent = 0;
insert( item );
myLast = myCurrent;
}
Dequeue Operation
void apqueue<E>::dequeue(E &item )
{
assert(!empty() )
first();
item = remove();
if (empty() )
myLast = 0;
}
Assignment Operation
apqueue<E>& apqueue<E>::operator=(const apqueue<E>&rhs)
{
if ( this != &rhs )
{
makempty();
node *rptr;
for(rptr= rhs.myFirst; rptr! = 0; rptr = rptr->next)
enqueue( rptr->data);
}
return *this;
}
Introduction to trees and graphs

12/07/2021 CS201 36
Trees

12/07/2021 CS201 37
What is a tree?
• Trees are structures used to represent hierarchical
relationship
• Each tree consists of nodes and edges
• Each node represents an object
• Each edge represents the relationship between two nodes.

node
edge

12/07/2021 CS201 38
Some applications of Trees
Organization Chart Expression Tree

President
+
VP VP
Personnel Marketing * 5

3 2
Director Director
Customer Sales
Relation

12/07/2021 CS201 39
Terminology I
• For any two nodes u and v, if there is an edge
pointing from u to v, u is called the parent of v while
v is called the child of u. Such edge is denoted as (u,
v).
• In a tree, there is exactly one node without parent,
which is called the root. The nodes without children
are called leaves.
root

u
u: parent of v
v: child of u
v
12/07/2021 CS201
leaves 40
Terminology II
• In a tree, the nodes without children are
called leaves. Otherwise, they are called
internal nodes.

internal nodes

leaves

12/07/2021 CS201 41
Terminology III
• If two nodes have the same parent, they are siblings.
• A node u is an ancestor of v if u is parent of v or
parent of parent of v or …
• A node v is a descendent of u if v is child of v or child
of child of v or …

v and w are siblings


u and v are ancestors of x v w
v and x are descendents of u
x

12/07/2021 CS201 42
Terminology IV
• A subtree is any node together with all its
descendants.

T
A subtree of T
v v

12/07/2021 CS201 43
Terminology V
• Level of a node n: number of nodes on the path from root to
node n
• Height of a tree: maximum level among all of its node

Level 1

Level 2
height=4
Level 3
n

Level 4

12/07/2021 CS201 44
Binary Tree
• Binary Tree: Tree in which every node has at most 2
children
• Left child of u: the child on the left of u
• Right child of u: the child on the right of u

x: left child of u
u v y: right child of u
w: right child of v
x y z: left child of w
w
z
12/07/2021 CS201 45
Full binary tree
• If T is empty, T is a full binary tree of height 0.
• If T is not empty and of height h >0, T is a full binary
tree if both subtrees of the root of T are full binary
trees of height h-1.

Full binary tree


of height 3

12/07/2021 CS201 46
Property of binary tree (I)
• A full binary tree of height h has 2h-1 nodes
No. of nodes = 20 + 21 + … + 2(h-1)
= 2h – 1

Level 1: 20 nodes

Level 2: 21 nodes

Level 3: 22 nodes

12/07/2021 CS201 47
Property of binary tree (II)
• Consider a binary tree T of height h. The
number of nodes of T  2h-1

Reason: you cannot have more nodes than a full


binary tree of height h.

12/07/2021 CS201 48
Property of binary tree (III)
• The minimum height of a binary tree with n
nodes is log(n+1)

By property (II), n  2h-1


Thus, 2h  n+1
That is, h  log2 (n+1)

12/07/2021 CS201 49
Binary Tree ADT
setElem

getElem
setLeft, setRight

binary
getLeft, getRight
tree
isEmpty, isFull,
isComplete

makeTree

12/07/2021 CS201 50
Representation of a Binary Tree
• An array-based representation
• A reference-based representation

12/07/2021 CS201 51
An array-based representation
nodeNum item leftChild rightChild
–1: empty tree root
0 d 1 2 0
1 b 3 4
2 f 5 -1
3 a -1 -1
d 4 c -1 -1
5 e -1 -1

b f 6 ? ? ? free
7 ? ? ? 6
8 ? ? ?
a c e 9 ? ? ?
... ..... ..... ....

12/07/2021 CS201 52
Reference Based Representation
NULL: empty tree left element right

You can code this with a


class of three fields:
Object element;
BinaryNode left;
BinaryNode right;
d d

b f
b f
a c
a c
12/07/2021 CS201 53
Tree Traversal
• Given a binary tree, we may like to do some
operations on all nodes in a binary tree. For
example, we may want to double the value in
every node in a binary tree.
• To do this, we need a traversal algorithm
which visits every node in the binary tree.

12/07/2021 CS201 54
Ways to traverse a tree
• There are three main ways to traverse a tree:
– Pre-order:
• (1) visit node, (2) recursively visit left subtree, (3) recursively visit
right subtree
– In-order:
• (1) recursively visit left subtree, (2) visit node, (3) recursively right
subtree
– Post-order:
• (1) recursively visit left subtree, (2) recursively visit right subtree,
(3) visit node
– Level-order:
• Traverse the nodes level by level
• In different situations, we use different traversal
algorithm.

12/07/2021 CS201 55
Examples for expression tree
• By pre-order, (prefix)
+*23/84
• By in-order, (infix)
+
2*3+8/4
• By post-order, (postfix) * /
23*84/+
2 3 8 4
• By level-order,
+*/2384
• Note 1: Infix is what we read!
• Note 2: Postfix expression can be computed
efficiently using stack

12/07/2021 CS201 56
Pre-order
Algorithm pre-order(BTree x)
If (x is not empty) {
print x.getItem(); // you can do other things!
pre-order(x.getLeftChild());
pre-order(x.getRightChild());
}

12/07/2021 CS201 57
Pre-order example

Pre-order(a); Print a; Print b; Print d;


Pre-order(b); Pre-order(d); Pre-order(null);
Pre-order(c); Pre-order(null); Pre-order(null);

Print c;
Pre-order(null);
Pre-order(null);

a b d c b c

d
12/07/2021 CS201 58
Time complexity of Pre-order
Traversal
• For every node x, we will call
pre-order(x) one time, which performs O(1)
operations.
• Thus, the total time = O(n).

12/07/2021 CS201 59
In-order and post-order
Algorithm in-order(BTree x)
If (x is not empty) {
in-order(x.getLeftChild());
print x.getItem(); // you can do other things!
in-order(x.getRightChild());
}

Algorithm post-order(BTree x)
If (x is not empty) {
post-order(x.getLeftChild());
post-order(x.getRightChild());
print x.getItem(); // you can do other things!
}

12/07/2021 CS201 60
In-order example

In-order(a); In-order(b); In-order(d); In-order(null);


Print a; Print b; Print d;
In-order(c); In-order(null); In-order(null);

In-order(null);
Print c;
In-order(null);

d b a c b c

12/07/2021 CS201 61
Post-order example

Post-order(a); Post-order(b); Post-order(d); Post-order(null);


Post-order(c); Post-order(null); Post-order(null);
Print a; Print b; Print d;

Post-order(null);
Print c;
Post-order(null);

d b c a b c

d
12/07/2021 CS201 62
Time complexity for in-order and
post-order
• Similar to pre-order traversal, the time
complexity is O(n).

12/07/2021 CS201 63
Level-order
• Level-order traversal requires a queue!
Algorithm level-order(BTree t)
Queue Q = new Queue();
BTree n;
Q.enqueue(t); // insert pointer t into Q
while (! Q.empty()){
n = Q.dequeue(); //remove next node from the front of Q
if (!n.isEmpty()){
print n.getItem(); // you can do other things
Q.enqueue(n.getLeft()); // enqueue left subtree on rear of Q
Q.enqueue(n.getRight()); // enqueue right subtree on rear of Q
};
};

12/07/2021 CS201 64
Time complexity of Level-order
traversal
• Each node will enqueue and dequeue one
time.
• For each node dequeued, it only does one
print operation!
• Thus, the time complexity is O(n).

12/07/2021 CS201 65
General tree implementation
struct TreeNode A
{
Object element
TreeNode *firstChild B C D E
TreeNode *nextsibling
}
F G
because we do not know how many children a
node has in advance.

• Traversing a general tree is similar to traversing a


binary tree

12/07/2021 CS201 66
Summary
• We have discussed
– the tree data-structure.
– Binary tree vs general tree
– Binary tree ADT
• Can be implemented using arrays or references
– Tree traversal
• Pre-order, in-order, post-order, and level-order

12/07/2021 CS201 67
Graphs

12/07/2021 CS201 68
What is a graph?
• Graphs represent the relationships among data items
• A graph G consists of
– a set V of nodes (vertices)
– a set E of edges: each edge connects two nodes
• Each node represents an item
• Each edge represents the relationship between two
items

node
edge

12/07/2021 CS201 69
Examples of graphs
Molecular Structure Computer Network
H Server 1 Terminal 1

H C H
Terminal 2
H Server 2

Other examples: electrical and communication networks,


airline routes, flow chart, graphs for planning projects

12/07/2021 CS201 70
Formal Definition of graph
• The set of nodes is denoted as V
• For any nodes u and v, if u and v are connected by
an edge, such edge is denoted as (u, v)
v
(u, v)

• The set of edges is denoted as E u


• A graph G is defined as a pair (V, E)

12/07/2021 CS201 71
Adjacent
• Two nodes u and v are said to be adjacent if
(u, v)  E

v
(u, v)
u
w
u and v are adjacent
v and w are not adjacent

12/07/2021 CS201 72
Path and simple path
• A path from v1 to vk is a sequence of nodes v1,
v2, …, vk that are connected by edges (v1, v2),
(v2, v3), …, (vk-1, vk)
• A path is called a simple path if every node
appears at most once. v2 v
v1 3

- v2, v3, v4, v2, v1 is a path


v4 v5
- v2, v3, v4, v5 is a path, also it
is a simple path
12/07/2021 CS201 73
Cycle and simple cycle
• A cycle is a path that begins and ends at the
same node
• A simple cycle is a cycle if every node appears
at most once, except for the first and the last
nodes
v2
v1 v3
- v2, v3, v4, v5 , v3, v2 is a cycle
- v2, v3, v4, v2 is a cycle, it is
v4 v5
also a simple cycle
12/07/2021 CS201 74
Connected graph
• A graph G is connected if there exists path
between every pair of distinct nodes;
otherwise, it is disconnected
v2
v1 v3

v4 v5
This is a connected graph because there exists path
between every pair of nodes
12/07/2021 CS201 75
Example of disconnected graph

v1 v3 v7 v8
v2
v4 v5
v6 v9

This is a disconnected graph because there does not


exist path between some pair of nodes, says, v1 and v7

12/07/2021 CS201 76
Connected component
• If a graph is disconnect, it can be partitioned into a
number of graphs such that each of them is
connected. Each such graph is called a connected
component.

v2 v7 v8
v1 v3

v4 v5
v6 v9

12/07/2021 CS201 77
Complete graph
• A graph is complete if each pair of distinct
nodes has an edge

Complete graph Complete graph


with 3 nodes with 4 nodes

12/07/2021 CS201 78
Subgraph
• A subgraph of a graph G =(V, E) is a graph H =
(U, F) such that U  V and
F  E.
v2 v2
v1 v3 v3

v4 v5 v4 v5

G H

12/07/2021 CS201 79
Weighted graph
• If each edge in G is assigned a weight, it is
called a weighted graph

Chicago 1000 New York

3500
2000

Houston

12/07/2021 CS201 80
Directed graph (digraph)
• All previous graphs are undirected graph
• If each edge in E has a direction, it is called a directed edge
• A directed graph is a graph where every edges is a directed
edge

Chicago 1000 New York

Directed edge
2000
3500

Houston
12/07/2021 CS201 81
More on directed graph
x y

• If (x, y) is a directed edge, we say


– y is adjacent to x
– y is successor of x
– x is predecessor of y
• In a directed graph, directed path, directed
cycle can be defined similarly

12/07/2021 CS201 82
Multigraph
• A graph cannot have duplicate edges.
• Multigraph allows multiple edges and self
edge (or loop).

Self edge Multiple edge

12/07/2021 CS201 83
Property of graph
• A undirected graph that is connected and has
no cycle is a tree.
• A tree with n nodes have exactly n-1 edges.
• A connected undirected graph with n nodes
must have at least n-1 edges.

12/07/2021 CS201 84
Implementing Graph
• Adjacency matrix
– Represent a graph using a two-dimensional array
• Adjacency list
– Represent a graph using n linked lists where n is
the number of vertices

12/07/2021 CS201 85
Adjacency matrix for directed graph
Matrix[i][j] = 1 if (vi, vj)E 1 2 3 4 5
0 if (vi, vj)E
v1 v2 v3 v4 v5
1 v1 0 1 0 0 0
v2
v1 v3 2 v 0 0 0 1 0
2

3 v3 0 1 0 1 0
v4 v5 4 v4 0 0 0 0 0
5 v5 0 0 1 1 0
G
12/07/2021 CS201 86
Adjacency matrix for weighted
undirected graph
Matrix[i][j] = w(vi, vj) if (vi, vj)E or (vj, vi)E
∞ otherwise
1 2 3 4 5
v2 v1 v2 v3 v4 v5
v1 2 v3
5 1 v1 ∞ 5 ∞ ∞ ∞
4 3 7 2 v2 5 ∞ 2 4 ∞
v4
8 v5
3 v3 0 2 ∞ 3 7
G 4 v4 ∞ 4 3 ∞ 8
5 v5 ∞ ∞ 7 8 ∞
12/07/2021 CS201 87
Adjacency list for directed graph

1 v1  v2
v2 2 v2  v4
v1 v3
3 v3  v2  v4
4 v4
v4 v5
5 v5  v3  v4
G

12/07/2021 CS201 88
Adjacency list for weighted undirected
graph

v2
v1 2 v3 1 v1  v2(5)
5
4 2 v2  v1(5)  v3(2)  v4(4)
3 7
3 v3  v2(2)  v4(3)  v5(7)
v4
8 v5
4 v4  v2(4)  v3(3)  v5(8)
G 5 v5  v3(7)  v4(8)

12/07/2021 CS201 89
Pros and Cons
• Adjacency matrix
– Allows us to determine whether there is an edge
from node i to node j in O(1) time
• Adjacency list
– Allows us to find all nodes adjacent to a given
node j efficiently
– If the graph is sparse, adjacency list requires less
space

12/07/2021 CS201 90
Problems related to Graph
• Graph Traversal
• Topological Sort
• Spanning Tree
• Minimum Spanning Tree
• Shortest Path

12/07/2021 CS201 91
Graph Traversal Algorithm
• To traverse a tree, we use tree traversal algorithms
like pre-order, in-order, and post-order to visit all the
nodes in a tree
• Similarly, graph traversal algorithm tries to visit all
the nodes it can reach.
• If a graph is disconnected, a graph traversal that
begins at a node v will visit only a subset of nodes,
that is, the connected component containing v.

12/07/2021 CS201 92
Two basic traversal algorithms
• Two basic graph traversal algorithms:
– Depth-first-search (DFS)
• After visit node v, DFS strategy proceeds along a path
from v as deeply into the graph as possible before
backing up
– Breadth-first-search (BFS)
• After visit node v, BFS strategy visits every node
adjacent to v before visiting any other nodes

12/07/2021 CS201 93
Depth-first search (DFS)
• DFS strategy looks similar to pre-order. From a given node v, it
first visits itself. Then, recursively visit its unvisited neighbours
one by one.
• DFS can be defined recursively as follows.

Algorithm dfs(v)
print v; // you can do other things!
mark v as visited;
for (each unvisited node u adjacent to v)
dfs(u);

12/07/2021 CS201 94
DFS example
• Start from v3
1
v3

2
v2 v2
v1 v3
x x x 3 4
v1 v4
v4
x x v5
5
G v5

12/07/2021 CS201 95
Non-recursive version of DFS
algorithm
Algorithm dfs(v)
s.createStack();
s.push(v);
mark v as visited;
while (!s.isEmpty()) {
let x be the node on the top of the stack s;
if (no unvisited nodes are adjacent to x)
s.pop(); // blacktrack
else {
select an unvisited node u adjacent to x;
s.push(u);
mark u as visited;
}
}

12/07/2021 CS201 96
Non-recursive DFS example
visit stack
v3 v3
v2
v2 v3, v2
v1 v3
v1 v 3 , v 2, v 1
x x x
backtrack v3, v2
v4 v 3 , v 2, v 4
v4
x x v5
v5 v 3 , v 2, v 4 , v 5
backtrack v 3 , v 2, v 4
backtrack v3, v2 G
backtrack v3
backtrack empty
12/07/2021 CS201 97
Breadth-first search (BFS)
• BFS strategy looks similar to level-order. From a given
node v, it first visits itself. Then, it visits every node
adjacent to v before visiting any other nodes.
– 1. Visit v
– 2. Visit all v’s neigbours
– 3. Visit all v’s neighbours’ neighbours
– …
• Similar to level-order, BFS is based on a queue.

12/07/2021 CS201 98
Algorithm for BFS
Algorithm bfs(v)
q.createQueue();
q.enqueue(v);
mark v as visited;
while(!q.isEmpty()) {
w = q.dequeue();
for (each unvisited node u adjacent to w) {
q.enqueue(u);
mark u as visited;
}
}

12/07/2021 CS201 99
BFS example
• Start from v5 Visit Queue
(front to
1 back)
v5 v5 v5

v2 v3 empty
v1
x x
2 3 v3 v3
v3 v4
x v4 v3, v4

v4x
v4
x
4
v2 v2 v4, v2
v5 v2
G 5 empty
v1 v1 v1

12/07/2021 CS201
empty
100
Topological order
• Consider the prerequisite structure for courses:

b d
a

c e
• Each node x represents a course x
• (x, y) represents that course x is a prerequisite to course y
• Note that this graph should be a directed graph without cycles (called a
directed acyclic graph).
• A linear order to take all 5 courses while satisfying all prerequisites is
called a topological order.
• E.g.
– a, c, b, e, d
– c, a, b, e, d

12/07/2021 CS201 101


Topological sort
• Arranging all nodes in the graph in a topological
order

Algorithm topSort
n = |V|;
for i = 1 to n {
select a node v that has no successor;
aList.add(1, v);
delete node v and its edges from the graph;
}
return aList;

12/07/2021 CS201 102


Example
b d b
a a

c e c e
1. d has no 2. Both b and e have no
successor! successor! Choose e!
Choose d!
b b
a a
a
c
3. Both b and c have 4. Only b has no 5. Choose a!
no successor! successor! The topological order
Choose c! Choose b! is a,b,c,e,d

12/07/2021 CS201 103


Topological sort algorithm 2
• This algorithm is based on DFS
Algorithm topSort2
s.createStack();
for (all nodes v in the graph) {
if (v has no predecessors) {
s.push(v);
mark v as visited;
}
}
while (!s.isEmpty()) {
let x be the node on the top of the stack s;
if (no unvisited nodes are adjacent to x) { // i.e. x has no unvisited successor
aList.add(1, x);
s.pop(); // blacktrack
} else {
select an unvisited node u adjacent to x;
s.push(u);
mark u as visited;
}
}
return aList;

12/07/2021 CS201 104


Spanning Tree
• Given a connected undirected graph G, a
spanning tree of G is a subgraph of G that
contains all of G’s nodes and enough of its
edges to form a tree.
v2
v1 v3

v4 v5
Spanning
tree Spanning tree is not unique!

12/07/2021 CS201 105


DFS spanning tree
• Generate the spanning tree edge during the DFS
traversal.

Algorithm dfsSpanningTree(v)
mark v as visited;
for (each unvisited node u adjacent to v) {
mark the edge from u to v;
dfsSpanningTree(u);
}

• Similar to DFS, the spanning tree edges can be generated


based on BFS traversal.

12/07/2021 CS201 106


Example of generating spanning
tree based on DFS
stack
v3 v3
v2
v2 v3, v2
v1 v3
v1 v 3 , v 2, v 1
x x x
backtrack v3, v2
v4 v 3 , v 2, v 4
v4
x x v5
v5 v 3 , v 2, v 4 , v 5
backtrack v 3 , v 2, v 4
backtrack v3, v2 G
backtrack v3
backtrack empty
12/07/2021 CS201 107
Minimum Spanning Tree
• Consider a connected undirected graph where
– Each node x represents a country x
– Each edge (x, y) has a number which measures the cost of
placing telephone line between country x and country y
• Problem: connecting all countries while minimizing
the total cost
• Solution: find a spanning tree with minimum total
weight, that is, minimum spanning tree

12/07/2021 CS201 108


Formal definition of minimum
spanning tree
• Given a connected undirected graph G.
• Let T be a spanning tree of G.
• cost(T) = eTweight(e)
• The minimum spanning tree is a spanning tree T
which minimizes cost(T)
v2
v1 2 v3
5 Minimum
4 3 spanning
7
tree
v4
8 v5
12/07/2021 CS201 109
Prim’s algorithm (I)
v2 v2 v2
v1 2 v3 v1 2 v3 v1 2 v3
5 5 5
4 3 4 3 4 3
7 7 7
v4 8 v5 v4 8 v5 v4 8 v5
Start from v5, find the Find the minimum edge Find the minimum edge
minimum edge attach to v5 attach to v3 and v5 attach to v2, v3 and v5

v2 v1 v2
v1 2 v3 2 v3
5 5
4 3 4 3 7
7 8
v4 8 v5 v4 v5

Find the minimum edge


attach to v2, v3 , v4 and v5

12/07/2021 CS201 110


Prim’s algorithm (II)
Algorithm PrimAlgorithm(v)
• Mark node v as visited and include it in the minimum
spanning tree;
• while (there are unvisited nodes) {
– find the minimum edge (v, u) between a visited node v and
an unvisited node u;
– mark u as visited;
– add both v and (v, u) to the minimum spanning tree;
}

12/07/2021 CS201 111


Shortest path
• Consider a weighted directed graph
– Each node x represents a city x
– Each edge (x, y) has a number which represent the cost of
traveling from city x to city y
• Problem: find the minimum cost to travel from city x
to city y
• Solution: find the shortest path from x to y

12/07/2021 CS201 112


Formal definition of shortest path
• Given a weighted directed graph G.
• Let P be a path of G from x to y.
• cost(P) = ePweight(e)
• The shortest path is a path P which minimizes cost(P)
v2
v1 2 v3
5
4 3 Shortest Path
4
v4
8 v5

12/07/2021 CS201 113


Dijkstra’s algorithm
• Consider a graph G, each edge (u, v) has a
weight w(u, v) > 0.
• Suppose we want to find the shortest path
starting from v1 to any node vi
• Let VS be a subset of nodes in G
• Let cost[vi] be the weight of the shortest path
from v1 to vi that passes through nodes in VS
only.

12/07/2021 CS201 114


Example for Dijkstra’s algorithm
v1 v2 2 v3
5
4 3 4
v4
8 v5
v VS cost[v1] cost[v2] cost[v3] cost[v4] cost[v5]
1 [v1] 0 5 ∞ ∞ ∞

12/07/2021 CS201 115


Example for Dijkstra’s algorithm
v2
v1 2 v3
5
4 3 4
v4
8 v5
v VS cost[v1] cost[v2] cost[v3] cost[v4] cost[v5]
1 [v1] 0 5 ∞ ∞ ∞
2 v2 [v1, v2] 0 5 ∞ 9 ∞

12/07/2021 CS201 116


Example for Dijkstra’s algorithm
v2
v1 2 v3
5
4 3 4
v4
8 v5
v VS cost[v1] cost[v2] cost[v3] cost[v4] cost[v5]
1 [v1] 0 5 ∞ ∞ ∞
2 v2 [v1, v2] 0 5 ∞ 9 ∞
3 v4 [v1, v2, v4] 0 5 12 9 17

12/07/2021 CS201 117


Example for Dijkstra’s algorithm
v2
v1 2 v3
5
4 3 4
v4
8 v5
v VS cost[v1] cost[v2] cost[v3] cost[v4] cost[v5]
1 [v1] 0 5 ∞ ∞ ∞
2 v2 [v1, v2] 0 5 ∞ 9 ∞
3 v4 [v1, v2, v4] 0 5 12 9 17
4 v3 [v1, v2, v4, v3] 0 5 12 9 16
5 v5 [v1, v2, v4, v3, v5] 0 5 12 9 16

12/07/2021 CS201 118


Dijkstra’s algorithm
Algorithm shortestPath()
n = number of nodes in the graph;
for i = 1 to n
cost[vi] = w(v1, vi);
VS = { v1 };
for step = 2 to n {
find the smallest cost[vi] s.t. vi is not in VS;
include vi to VS;
for (all nodes vj not in VS) {
if (cost[vj] > cost[vi] + w(vi, vj))
cost[vj] = cost[vi] + w(vi, vj);
}
}

12/07/2021 CS201 119


Summary
• Graphs can be used to represent many real-life
problems.
• There are numerous important graph algorithms.
• We have studied some basic concepts and
algorithms.
– Graph Traversal
– Topological Sort
– Spanning Tree
– Minimum Spanning Tree
– Shortest Path

12/07/2021 CS201 120

You might also like