You are on page 1of 65

Data Structures and Algorithms

Graphs
Prepared by Kristian Guillaumier
Department of Intelligent Computer Systems

Note on the slides


Any code or pseudo-code shown in these
slides may not be the most efficient or optimal
way to implement an algorithm. The purpose
of code in these slides is to support an
argument or an explanation in class.
Send corrections to
kristian.guillaumier@um.edu.mt

Revision: Trees
Simple binary tree from Wikipedia:

Tree Data Structures


Operations:
Initialising the tree.
Insert children.
Traversing the tree.
Traversing a sub tree.
Deleting an item.
Searching for an item.

Binary Tree
class Node
{
public int Value
public Node LeftChild
public Node RightChild
}

= 0;
= null;
= null;

Binary Tree
static Node CreateNode(int value)
{
Node newNode
= new Node();
newNode.Value
= value;
newNode.LeftChild
= null;
newNode.RightChild = null;
//
return newNode;
}

Binary Tree
static void Main(string[] args)
{
Node root
=
root.LeftChild
=
root.RightChild
=
root.LeftChild.LeftChild
=
root.LeftChild.RightChild
=
root.RightChild.LeftChild
=

CreateNode(10);
CreateNode(3);
CreateNode(44);
CreateNode(11);
CreateNode(8);
CreateNode(99);

Console.ReadLine();
}

Binary Tree Preorder Traversal


static void PrintTree(Node node, int depth)
{
if (node != null)
{
Console.Write(new string(' ', depth));
Console.WriteLine(node.Value);
PrintTree(node.LeftChild, depth + 1);
PrintTree(node.RightChild, depth + 1);
}
else
{
Console.Write(new string(' ', depth));
Console.WriteLine("-");
}

Binary Tree Preorder Traversal

Binary Tree Post Order Traversal


static void PrintTreePost(Node node, int depth)
{
if (node != null)
{
PrintTreePost(node.LeftChild, depth + 1);
PrintTreePost(node.RightChild, depth + 1);
Console.Write(new string(' ', depth));
Console.WriteLine(node.Value);
}
else
{
Console.Write(new string(' ', depth));
Console.WriteLine("?");
}

10

Binary Tree Post Order Traversal

11

Binary Tree Postorder Traversal


Useful to convert an expression in prefix
notation (Polish notation).
Example from Wikipedia (2+3)*4:

12

Binary Tree In Order Traversal


static void PrintTreeIn(Node node, int depth)
{
if (node != null)
{
PrintTreeIn(node.LeftChild, depth + 1);

Console.Write(new string(' ', depth));


Console.WriteLine(node.Value);
PrintTreeIn(node.RightChild, depth + 1);
}
else
{
Console.Write(new string(' ', depth));
Console.WriteLine("?");
}
}

13

Binary Tree In Order Traversal

14

Binary Tree In Order Traversal


If we do an in order traversal of a binary
search tree, the sequence of items returned
will be sorted.

15

Binary Tree Breadth First


static void PrintTreeBF(Node node)
{
int counter
= 0;
Queue<Node> q
= new Queue<Node>();
q.Enqueue(node);
while (q.Count > 0)
{
Node n = q.Dequeue();
Console.Write(counter);
Console.Write(":");
Console.WriteLine(n.Value);

if (n.LeftChild != null) q.Enqueue(n.LeftChild);


if (n.RightChild != null) q.Enqueue(n.RightChild);
counter++;
}
}

16

Binary Tree Breadth First

17

Binary Search Tree


In a BST all the nodes in the left sub tree have a value less than the value
of the current node.
All the nodes in the right sub tree have a value greater than or equal to
the value of the current node.
From Wikipedia:

18

Binary Search Tree


static void InsertValue(Node node, Node newNode)
{
if (newNode.Value < node.Value)
{
if (node.LeftChild == null)
{
node.LeftChild = newNode;
}
else
{
InsertValue(node.LeftChild, newNode);
}
}
else
{
if (node.RightChild == null)
{
node.RightChild = newNode;
}
else
{
InsertValue(node.RightChild, newNode);
}
}
}

19

Binary Search Tree

20

Binary Search Tree


static bool BstFind(Node node, int value)
{
if (node == null)
{
return false;
}
else
{
if (value == node.Value)
return true;
else if (value < node.Value)
return BstFind(node.LeftChild, value);
else
return BstFind(node.RightChild, value);
}
}

21

Degenerate Trees and Analysis


Degenerate tree: for every
parent, only one child is
associated.
In worst case, tree is
degenerate linear search
O(n).
In average case, tree is
perfectly balanced
O(log2n).
22

Self Balancing Binary Trees


Attempt to keep the depth of the tree at a
minimum (avoid degeneration).
Algorithms use tree rotations. From
Wikipedia:

23

LL Rotation (From Wikipedia)

24

LLRotate
static void LLRotate(Node node)
{
Node temp
=
node.RightChild
=
node.LeftChild
=
node.RightChild.LeftChild
=
node.RightChild.RightChild =
//
int tempVal
node.Value
node.RightChild.Value

node.RightChild;
node.LeftChild;
node.RightChild.LeftChild;
node.RightChild.RightChild;
temp;

= node.Value;
= node.RightChild.Value;
= tempVal;

25

RR Rotation

26

RRRotate
static void RRRotate(Node node)
{
Node temp
=
node.LeftChild
=
node.RightChild
=
node.LeftChild.RightChild
=
node.LeftChild.LeftChild
=
//
int tempVal
node.Value
node.LeftChild.Value

node.LeftChild;
node.RightChild;
node.LeftChild.RightChild;
node.LeftChild.LeftChild;
temp;

= node.Value;
= node.LeftChild.Value;
= tempVal;

27

LR Rotation

28

RL Rotation

29

LRRotate/RLRotate
static void LRRotate(Node node)
{
RRRotate(node.LeftChild);
LLRotate(node);
}
static void RLRotate(Node node)
{
LLRotate(node.RightChild);
RRRotate(node);
}
30

Another Nice Site


http://www.cs.bham.ac.uk/~mhe/foundations
2/node1.html

31

Graphs
Some graph notes and examples are taken from:
http://www.brpreiss.com/books/opus6/html/page522
.html

ADT:

Collection of nodes/vertices.
Nodes are connected by edges/arcs.
G = (V, E).
E = (V1, V2).
Example Operations:
Finding a path between nodes.
32

Basics
Ordered pair:

Collection of 2 objects a and b.


Written as (a, b).
(a, b) is different from (b, a).
a is distinguished as the first item.
b is distinguished as the second.
a can be = b.

Set:
A sequence of elements:
Order and repetitions are irrelevant.
{a, b} = {b, a} = {b, a, b}.
33

Directed Graphs
Ordered pair G = (V, E)
V: finite, non-empty set of vertices.
E: finite, non-empty set of ordered pairs called
edges.
EVV

Note that an edge an ordered pair of


vertices (a, c) is distinct from (c, a).
34

Example
G = (V1, E1)
Where:
V1 = {a, b, c, d}
E1 = {(a,b), (a,c), (b,c) }
Giving:

35

Basics
Sometimes an edge (v, w) E is written v w.
The arrow is called a directed arc.
w is the head of the arc.
v is the tail of the arc.

Vertex w is adjacent to vertex v.


An edge v w is said to emanate from v.
The notation A(v) is used to denote the set of edges
emanating from node v.
An edge v w is said to be incident to w.
The notation I(w) is used to denote the set of edges
incident to node w.
36

Basics
Order of a graph:
|V| = number of vertices.

Size of a graph:
|E| = number of edges.

Out-Degree of a vertex:
The number of edges emanating from a node.
Written as |A(v)|

In-Degree of a vertex:
The number of edges incident on a node.
Written as |I(v)|
37

Basics

38

Basics
Inverted edges:
If (u, v) is an edge e1 then e2 = (v, u) is e1
inverted.

Pseudograph/Multigraph:
Two vertices may be connected by more than one
edge e.g. vertices a and b and edges ((b,a), (b,a)).

39

Walks
A walk in a graph G is a non-empty sequence of
vertices:
P = {v1, v2, v3, , vk}
The length of a walk is the number of edges in the
walk.
Open walk: start and end vertices are not the same.
Closed walk: start and end vertices are the same.
The length of an open walk is (n-1) where n is the
number of vertices visited.
The length of a closed walk is (n) where n is the
number of vertices visited.
40

Basics
Each vertex vi in a walk/path has a successor vi+1 except
the last.
Each vertex vi in a walk/path has a predecessor vi-1
except the first.
A walk is simple iff vi != vj such that:
1 <= i <= j <= k.
It is permissible for v1 = vk. In this case the walk is closed.
Otherwise it is an open walk.

{a, b, c, d} is a simple walk.


{a, b, c, a} is a simple closed walk.
{a, b, a, d} is not a simple walk.
41

Paths
A path is a simple walk (no vertex is repeated).
May be open or closed.

42

Definitions
Cycle:
Closed path no vertex is repeated except start and
end vertices. v1 = vk.
The length of a cycle is the length of the path.

Loops:
A loop is a cycle whose length = 1 i.e. {v,v}

Acyclic:
A graph without cycles.
A tree is a connected, directed, simple, acyclic graph.
43

Basics
Subgraph:
The subgraph S of a graph G is one where:
Each vertex in S is a vertex in V of G.
Each edge in S:
Is a subset of the edges in G.
Each vertex in the edges of S is a vertex in S.

Connected graph:
Every pair of vertices is connected by an edge.

44

Definitions
Undirected graphs:
An edge is not an ordered pair but a set of 2
vertices.

Directed Graph/Digraph:
D = (V, E), such that:
D = set of vertices.
E = ordered pair of vertices.

An edge (u, v) therefore has a direction.


u is the head of the edge.
v is the tail.
45

Directed Acyclic Graphs


Directed and has no cycles.
Note: a tree is a DAG but not all DAGs are
trees.

46

Undirected Graphs
Vertices are connected by undirected arcs:
No arrow.
No head or tail.
An edge is a set not an ordered pair.
{v,w} = {w,v}.
E.g. V={a,b,c,d}, E={{a,b}, {a,c},{b,c},{c,d}}

47

Labelled and Weighted Graphs


An annotation on edges or vertices.

48

Labelled and Weighted Graphs


To represent an FSM.
Geographic info e.g. distance between cities
(e.g. TSP).
Cost.

49

Basics
Complete Graph:
Simple graph (no loops).
Any pair of vertices is connected by an edge.
G complete graph with n vertices has n(n-1)/2 edges.

Planar Graph:
Can be drawn on a plane without any edges
intersecting.
Applications:
Wiring microprocessor circuits.
50

Graph Representation
Note that in a directed graph G=(V,E),
We can have |V|2 possible edges:
1 vertex = 1 loop.
2 vertices = 1 loop, 1 normal, 1 normal, 1 loop.

51

Graph Representation

Adjacency Matrix:
Consider G=(V,E), V=(v1,v2,vn).
We can use a n by n matrix A of 1s and 0s where:
Ai,j = 1 if (vi,vj) E,
0 otherwise.

52

Graph Representation
Common representations are the adjacency list and the
adjacency matrix.
Suppose the vertices of a graph are labeled with
numbers from 0 to (n-1).
Adjacency matrix:
An adjacency matrix would then be a 2D array (n by n) of
Boolean values.
A[i, j] true if there is an edge from i to j.
If the graph is weighted, then a value in the matrix is the
weight.
If the graph is undirected A[i, j] = A[j, i]. This will make the
matrix symmetric about the diagonal.
53

Graph Representation
In adjacency matrices, space is O(|V|2).
Irrespective of the number of edges in the
graph.
If |E| << |V|2 then the matrix will be sparse
and will be inefficient most of it will be
zeros.
Sparse vs dense.

54

Dense/Sparse Graphs
Dense: number of edges is close to the
maximum number of edges.
A complete graph is the most dense.
A sparse graph is the opposite.
Which implementation is best for a sparse
graph:
Hint: An adjacency matrix always has n2 elements.

55

Graph Representation
Adjacency list:
Linked list of linked lists.
For a graph with n vertices, the primary linked list
has n elements. I.e. each vertex is a linked list of
adjacent vertices.
Example:
ABC
BC
C
DC
56

Suitability
Consider operation of finding if there is an edge between two
vertices v1 and v2.
In adjacency matrix:
Examine value of G[V1, V2].

In adjacency list:
Locate V1 in primary list, look for V2 in secondary list.

Winner is: adjacency matrix.

Find all vertices adjacent to a vertex V1 in a graph with n vertices.


In adjacency matrix:
Go to the V1 row.
Loop n times and scan each column.

In adjacency list:
Go the V1 element.
The secondary list is the list of adjacent vertices.

Winner is: adjacency list.


57

Topological Sort
Major application is job scheduling.
Consider example from:
https://sys.cs.rice.edu/course/comp314/07/le
ctures/11-topological-sort/

58

Topological Sort
Consider a DAG (directed, acyclic).
A natural ordering.
Consider the graph:
A
B
D

B
C
E

C
D
E

Intuitively we see that A comes before B. B comes before C.


Any vertex comes before those to which it has outbound vertices.
59

Algorithm
Repeat:
Find a vertex with no successor.
Remove it from graph.
Put it at beginning of list.

Until graph is empty.

60

Many Results (from Wikipedia)

61

Graph Traversals
Depth first:
Like depth first for a tree.
But a tree specifies a root and a graph does not. In
other words we have to specify a starting vertex.

Visit vertex and recursively visit adjacent vertices.


Problem is cycles and we must visit every node
only once + infinite loop.
Keep track of nodes visited.

DFS can be used to determine if graph is


connected.
62

Algorithm
-- Global
bool[] visited = new bool[number of vertices]
Set all visited to false.
DepthFirst(vertex v)
{
Visit v
visited[v.number] = true
for each successor s of v
{
if (visited[v.number] = false)
{
DepthFirst(s)
}
}
}

63

Breadth First Search


Start from v.
Put v on a queue.
Repeat until queue is empty:
Remove vertex at head of queue and call it w.
Visit w.
For each vertex z emanating from w
If z has never been enqueued, enqueue it.

64

Breadth First Search

65

You might also like