Download as pdf or txt
Download as pdf or txt
You are on page 1of 105

Unit 4 :

Non-linear Data Structure


Graph

What is a graph?
▪ A data structure that consists of a set of nodes (vertices) and a set
of edges that relate the nodes to each other

▪ The set of edges describes relationships among the vertices



Formal definition of graphs

▪ A graph G is defined as follows:

G=(V, E)

V(G): a finite, nonempty set of vertices

E(G): a set of edges (pairs of vertices)



Directed vs. Undirected graphs
▪ When the edges in a graph have no direction, the graph is
called undirected

Directed vs. undirected graphs (cont.)
▪ When the edges in a graph have a direction, the graph is called
directed (or digraph)

Warning: if the graph


is directed, the order
of the vertices in
each edge is
E(Graph2) = {(1,3) (3,1) (5,9) (9,11) (5,7)(9,9)(11,1)}
important !!
◤ Trees vs graphs
▪ Trees are special cases of graphs!!
▪ A connected graph with no cycles is called a tree.
Graph Terminology

A dj a ce n t Ve rt ice s a r e t wo

vertic e s t ha t a r e j oin e d by
an edge.

▪ Adjacent Edges are two


edg e s t h at in t er s ec t at a
vertex.

▪ Adjacent nodes/Vertices: two nodes are adjacent if they are


connected by an edge

5 is not adjacent to 7
7 is adjacent from 5
Graph Terminology

▪ The de g r ee o f a v erte x is
the number of edges at
that vertex.

Degree, In-degree, Out-degree

▪ Degree of every vertex is 3



Degree, In-degree, Out-degree
Graph Terminology

▪ A pa t h is a s e q u e nc e o f
vertices such that each
v er t e x is a dj a c e n t t o t h e
next. I n a p a t h , e a ch e dg e
ca n b e t r av e l e d o n l y o n ce .

e n gt h o f a p at h is t h e
• The l
num b er o f ed g e s in t h at
path.
Graph Terminology

• A p a t h t h at s ta r t s a n d
ends a t t h e s a m e v e r t e x is
c a l l ed a c ir c u it / c y c l e .

Cycle
Graph Terminology

C o m p l e te g r ap h : a g r a p h in
• A y
e v e r y v ert e x is d ire c t l
which
connected to every other
vertex

Graph terminology (cont.)
▪ What is the number of edges in a complete directed graph with N
vertices?

▪ Total number of edges:

N * (N-1)

Graph terminology (cont.)
▪ What is the number of edges in a complete undirected graph
with N vertices?

▪ Total number of edges:

N * (N-1) / 2
Graph Terminology

r a p h is c o n ne c t ed if a n y
• Ag
by
two vertices can be joined
t h . I f th is is n o t p os s ib l e
a pa
then the graph is
disconnected.

Connected vs. Disconnected Graph
Graph Terminology

eigh ted g r a p h : a gr ap h in
• AW
which each edge carries a
value

▪ Weighted graph: a graph in which each edge carries a value


A
1) Find the degree of D
each vertex.
2) Is A adjacent to B? C
Is D adjacent to A?
Is E adjacent to
itself?
Is C adjacent to
itself? E
3) Is AB adjacent to
BC? B
Is CE adjacent to
BD?

Graph implementation
▪ Array-based implementation
▪ A 1D array is used to represent the vertices
▪ A 2D array (adjacency matrix) is used to represent the edges

Array-based implementation

Linked-list implementation
▪ A 1D array is used to represent the vertices

▪ A list is used for each vertex v which contains the vertices


which are adjacent from v (adjacency list)

Linked-list implementation
27

Graph Representation

▪ For graphs to be computationally useful, they have to be


conveniently represented in programs

▪ There are two computer representations of graphs:

▪ Adjacency matrix representation

▪ Adjacency lists representation


CS 103
28

Adjacency Matrix Representation

▪ In this representation, each graph of n nodes is represented by an


n x n matrix A, that is, a two-dimensional array A

▪ The nodes are labeled as 1,2,…,n

▪ A[i][j] = 1 if (i,j) is an edge

▪ A[i][j] = 0 if (i,j) is not an edge


CS 103

Adjacency Matrix

1 2

3 4

5
◤ Adjacency Matrix of Directed and Undirected Graph

Adjacency Matrix for Weighted Graph
32

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
CS 103
33

Adjacency Lists Representation

▪ A graph of n nodes is represented by a one-dimensional array L of


linked lists, where
▪ L[i] is the linked list containing all the nodes adjacent from
node i.
▪ The nodes in the list L[i] are in no particular order
CS 103

Adjacency Lists Representation

Example(2)
0
0

1 2 1
3
2
0 1 2 3
1 0 2 3
0 1

2 0 1 3
1 0 2

0 1 2
2
3

G G
1 2

Example(3)

0 1 2
0 4 0 3
1
2 1 5 2 0 3

3 6 3 1 2

4 5

7 5 4 6

6 5 7

7 6

An undirected graph with n vertices and e edges ==> n head nodes



Graph Traversal Orders

▪ Depth-first search (DFS)

- Implemented using Stack

▪ Breadth-first search (BFS)


- Implemented using Stack

Depth-first search (DFS)

Step 1: Initialize each node in G in ready state

Step 2: Push the starting node A on the stack

Step 3: Repeat Steps 4 and 5 until STACK is empty

Step 4: Pop the top node N. Process it and set its status as processed
state

Step 5: Push on the stack all the neighbors of N

[END OF LOOP]

Step 6: EXIT

Breadth-first search (BFS)

Step 1: Initialize each node in G in ready state

Step 2: Enqueue the starting node A in the queue

Step 3: Repeat Steps 4 and 5 until queue is empty

Step 4: Dequeue the front element node N. Process it and set its
status as processed state

Step 5: Enqueue in the queue all the neighbors of N

[END OF LOOP]

Step 6: EXIT

Graph Traversal Orders
The order we explore the vertices depends upon the data
structure used to hold the discovered vertices yet to be fully
explored:
∙ Queue - by storing the vertices in a first-in, first out (FIFO)
queue, we explore the oldest unexplored vertices first. Thus
we radiate out slowly from the starting vertex, defining a so-
called breadth-first search.
∙ Stack - by storing the vertices in a last-in, first-out (LIFO)
stack, we explore the vertices by constantly visiting a new
neighbor if one is available; we back up only when
surrounded by previously discovered vertices. This defines
a so-called depth-first search.
41

Breadth First Search
2 4 8

s 5 7

3 6 9
42

Breadth First Search
Shortest path 1
from s
2 4 8

0 s 5 7

3 6 9

Undiscovered
Discovered Queue: s

Top of queue
Finished
43

Breadth First Search
1
2 4 8

0 s 5 7

3 6 9

Undiscovered
Discovered Queue: s 2

Top of queue
Finished
44

Breadth First Search
1
2 4 8

0 s 5 7
1

3 6 9

Undiscovered
Discovered Queue: s 2 3

Top of queue
Finished
45

Breadth First Search
1
2 4 8

0 s 5 7
1

3 6 9

Undiscovered
Discovered Queue: 2 3 5

Top of queue
Finished
46

Breadth First Search
1 2
2 4 8

0 s 5 7
1

3 6 9

Undiscovered
Discovered Queue: 2 3 5

Top of queue
Finished
47

Breadth First Search
1 2
2 4 8

5 already discovered:
0 s 5 7
don't enqueue
1

3 6 9

Undiscovered
Discovered Queue: 2 3 5 4

Top of queue
Finished
48

Breadth First Search
1 2
2 4 8

0 s 5 7
1

3 6 9

Undiscovered
Discovered Queue: 2 3 5 4

Top of queue
Finished
49

Breadth First Search
1 2
2 4 8

0 s 5 7
1

3 6 9

Undiscovered
Discovered Queue: 3 5 4

Top of queue
Finished
50

Breadth First Search
1 2
2 4 8

0 s 5 7
1

3 6 9

1 2

Undiscovered
Discovered Queue: 3 5 4

Top of queue
Finished
51

Breadth First Search
1 2
2 4 8

0 s 5 7
1

3 6 9

1 2

Undiscovered
Discovered Queue: 3 5 4 6

Top of queue
Finished
52

Breadth First Search
1 2
2 4 8

0 s 5 7
1

3 6 9

1 2

Undiscovered
Discovered Queue: 5 4 6

Top of queue
Finished
53

Breadth First Search
1 2
2 4 8

0 s 5 7
1

3 6 9

1 2

Undiscovered
Discovered Queue: 5 4 6

Top of queue
Finished
54

Breadth First Search
1 2
2 4 8

0 s 5 7
1

3 6 9

1 2

Undiscovered
Discovered Queue: 4 6

Top of queue
Finished
55

Breadth First Search
1 2 3
2 4 8

0 s 5 7
1

3 6 9

1 2

Undiscovered
Discovered Queue: 4 6

Top of queue
Finished
56

Breadth First Search
1 2 3
2 4 8

0 s 5 7
1

3 6 9

1 2

Undiscovered
Discovered Queue: 4 6 8

Top of queue
Finished
57

Breadth First Search
1 2 3
2 4 8

0 s 5 7
1 3

3 6 9

1 2

Undiscovered
Discovered Queue: 6 8

Top of queue
Finished
58

Breadth First Search
1 2 3
2 4 8

0 s 5 7
1 3

3 6 9

1 2 3

Undiscovered
Discovered Queue: 6 8 7

Top of queue
Finished
59

Breadth First Search
1 2 3
2 4 8

0 s 5 7
1 3

3 6 9

1 2 3

Undiscovered
Discovered Queue: 6 8 7 9

Top of queue
Finished
60

Breadth First Search
1 2 3
2 4 8

0 s 5 7
1 3

3 6 9

1 2 3

Undiscovered
Discovered Queue: 8 7 9

Top of queue
Finished
61

Breadth First Search
1 2 3
2 4 8

0 s 5 7
1 3

3 6 9

1 2 3

Undiscovered
Discovered Queue: 7 9

Top of queue
Finished
62

Breadth First Search
1 2 3
2 4 8

0 s 5 7
1 3

3 6 9

1 2 3

Undiscovered
Discovered Queue: 7 9

Top of queue
Finished
63

Breadth First Search
1 2 3
2 4 8

0 s 5 7
1 3

3 6 9

1 2 3

Undiscovered
Discovered Queue: 7 9

Top of queue
Finished
64

Breadth First Search
1 2 3
2 4 8

0 s 5 7
1 3

3 6 9

1 2 3

Undiscovered
Discovered Queue: 7 9

Top of queue
Finished
65

Breadth First Search
1 2 3
2 4 8

0 s 5 7
1 3

3 6 9

1 2 3

Undiscovered
Discovered Queue: 9

Top of queue
Finished
66

Breadth First Search
1 2 3
2 4 8

0 s 5 7
1 3

3 6 9

1 2 3

Undiscovered
Discovered Queue: 9

Top of queue
Finished
67

Breadth First Search
1 2 3
2 4 8

0 s 5 7
1 3

3 6 9

1 2 3

Undiscovered
Discovered Queue: 9

Top of queue
Finished
68

Breadth First Search
1 2 3
2 4 8

0 s 5 7
1 3

3 6 9

1 2 3

Undiscovered
Discovered Queue:

Top of queue
Finished
69

Breadth First Search
1 2 3
2 4 8

0 s 5 7
1 3

3 6 9

1 2 3

Level Graph
Minimum spanning trees

Definition

▪ A Minimum Spanning Tree (MST) is a subgraph of an undirected


graph such that the subgraph spans (includes) all nodes, is
connected, is acyclic, and has minimum total edge weight

Minimum Spanning Tree Algorithm

▪ Kruskal’s Algorithms

▪ Prim’s Algorithms

Algorithm Characteristics

▪ Both Prim’s and Kruskal’s Algorithms work with


undirected graphs
▪ Both work with weighted and unweighted graphs but are
more interesting when edges are weighted
▪ Both are greedy algorithms that produce optimal solutions
Minimum Connector Algorithms

Kruskal’s algorithm Prim’s algorithm

1. Select the shortest edge in a 1. Select any vertex


network
2. Select the shortest edge
2. Select the next shortest edge connected to that vertex
which does not create a cycle
3. Select the shortest edge
3. Repeat step 2 until all connected to any vertex
vertices have been connected already connected

4. Repeat step 3 until all


vertices have been
connected
Kruskal’s Algorithm

Work with edges, rather than nodes


Two steps:
– Sort edges by increasing edge weight
– Select the first |V| – 1 edges that do not
generate a cycle
◤ Example
A cable company want to connect five villages to their network
which currently extends to the market town of Avonford. What is the
minimum length of cable needed?

Brinleigh 5
Cornwell

3
4
8 6

8
Avonford Fingley Donster
7

5
4
2

Edan
We model the situation as a network, then the
problem is to find the minimum connector for the
network

B 5
C

3
4
8 6

8
A F D
7

5
4
2

E
Kruskal’s Algorithm

List the edges in


order of size:
B 5
C
ED 2
3 AB 3
4 AE 4
8 6
CD 4
BC 5
8
EF 5
A D
7 F CF 6
AF 7
5 BF 8
4 CF 8
2

E
Kruskal’s Algorithm

Select the shortest


edge in the network
B 5
C
ED 2
3
4
8 6

8
A D
7 F

5
4
2

E
Kruskal’s Algorithm

Select the next shortest


edge which does not
B 5 create a cycle
C

3 ED 2
4 AB 3
8 6

8
A D
7 F

5
4
2

E
Kruskal’s Algorithm

Select the next shortest


edge which does not
B 5 create a cycle
C

3
ED 2
4 AB 3
8 6
CD 4 (or AE 4)
8
A D
7 F

5
4
2

E
Kruskal’s Algorithm

Select the next shortest


edge which does not
B 5 create a cycle
C

3
ED 2
4 AB 3
8 6
CD 4
AE 4
8
A D
7 F

5
4
2

E
Kruskal’s Algorithm

Select the next shortest


edge which does not
B 5 create a cycle
C

3
ED 2
4 AB 3
8 6
CD 4
AE 4
8
BC 5 – forms a cycle
A D
7 F EF 5

5
4
2

E
Kruskal’s Algorithm

All vertices have been


connected.
B 5
C
The solution is
3
4 ED 2
8 6
AB 3
CD 4
8
AE 4
A D
7 F EF 5

5
4 Total weight of tree: 18
2

E
Walk-Through
Consider an undirected, weight graph
3
10
F C
A 4
4
3
8
6
5
4
B D
4
H 1
2
3
G 3
E
Sort the edges by increasing edge weight
3
10
F C edge d edge dv

A 4 3 v (B,E) 4
8 4 (D,E) 1
6 (B,F) 4
5
4
B D (D,G) 2 (B,H) 4
4 (E,G) 3
H 1
(A, 5
2 (C,D) 3 H)
3
G 3
E (G, 3 (D,F) 6
H) (A,B) 8
(C,F) 3 (A,F) 10
(B,C) 4
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge d edge dv

A 4 3 v (B,E) 4
8 4 (D,E) 1 √
6 (B,F) 4
5
4
B D (D,G) 2 (B,H) 4
4 (E,G) 3
H 1
(A, 5
2 (C,D) 3 H)
3
G 3
E (G, 3 (D,F) 6
H) (A,B) 8
(C,F) 3 (A,F) 10
(B,C) 4
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge d edge dv

A 4 3 v (B,E) 4
8 4 (D,E) 1 √
6 (B,F) 4
5
4
B D (D,G) 2 √ (B,H) 4
4 (E,G) 3
H 1
(A, 5
2 (C,D) 3 H)
3
G 3
E (G, 3 (D,F) 6
H) (A,B) 8
(C,F) 3 (A,F) 10
(B,C) 4
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge d edge dv

A 4 3 v (B,E) 4
8 4 (D,E) 1 √
6 (B,F) 4
5
4
B D (D,G) 2 √ (B,H) 4
4 (E,G) 3 χ
H 1
(A, 5
2 (C,D) 3 H)
3
G 3
E (G, 3 (D,F) 6
H) (A,B) 8
(C,F) 3 (A,F) 10
Accepting edge (E,G) would create a cycle
(B,C) 4
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge d edge dv

A 4 3 v (B,E) 4
8 4 (D,E) 1 √
6 (B,F) 4
5
4
B D (D,G) 2 √ (B,H) 4
4 (E,G) 3 χ
H 1
(A, 5
2 (C,D) 3 √ H)
3
G 3
E (G, 3 (D,F) 6
H) (A,B) 8
(C,F) 3 (A,F) 10
(B,C) 4
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge d edge dv

A 4 3 v (B,E) 4
8 4 (D,E) 1 √
6 (B,F) 4
5
4
B D (D,G) 2 √ (B,H) 4
4 (E,G) 3 χ
H 1
(A, 5
2 (C,D) 3 √ H)
3
G 3
E (G, 3 √ (D,F) 6
H) (A,B) 8
(C,F) 3 (A,F) 10
(B,C) 4
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge d edge dv

A 4 3 v (B,E) 4
8 4 (D,E) 1 √
6 (B,F) 4
5
4
B D (D,G) 2 √ (B,H) 4
4 (E,G) 3 χ
H 1
(A, 5
2 (C,D) 3 √ H)
3
G 3
E (G, 3 √ (D,F) 6
H) (A,B) 8
(C,F) 3 √ (A,F) 10
(B,C) 4
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge d edge dv

A 4 3 v (B,E) 4
8 4 (D,E) 1 √
6 (B,F) 4
5
4
B D (D,G) 2 √ (B,H) 4
4 (E,G) 3 χ
H 1
(A, 5
2 (C,D) 3 √ H)
3
G 3
E (G, 3 √ (D,F) 6
H) (A,B) 8
(C,F) 3 √ (A,F) 10
(B,C) 4 √
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge d edge dv

A 4 3 v (B,E) 4 χ
8 4 (D,E) 1 √
6 (B,F) 4
5
4
B D (D,G) 2 √ (B,H) 4
4 (E,G) 3 χ
H 1
(A, 5
2 (C,D) 3 √ H)
3
G 3
E (G, 3 √ (D,F) 6
H) (A,B) 8
(C,F) 3 √ (A,F) 10
(B,C) 4 √
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge d edge dv

A 4 3 v (B,E) 4 χ
8 4 (D,E) 1 √
6 (B,F) 4 χ
5
4
B D (D,G) 2 √ (B,H) 4
4 (E,G) 3 χ
H 1
(A, 5
2 (C,D) 3 √ H)
3
G 3
E (G, 3 √ (D,F) 6
H) (A,B) 8
(C,F) 3 √ (A,F) 10
(B,C) 4 √
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge d edge dv

A 4 3 v (B,E) 4 χ
8 4 (D,E) 1 √
6 (B,F) 4 χ
5
4
B D (D,G) 2 √ (B,H) 4 χ
4 (E,G) 3 χ
H 1
(A, 5
2 (C,D) 3 √ H)
3
G 3
E (G, 3 √ (D,F) 6
H) (A,B) 8
(C,F) 3 √ (A,F) 10
(B,C) 4 √
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge d edge dv

A 4 3 v (B,E) 4 χ
8 4 (D,E) 1 √
6 (B,F) 4 χ
5
4
B D (D,G) 2 √ (B,H) 4 χ
4 (E,G) 3 χ
H 1
(A, 5 √
2 (C,D) 3 √ H)
3
G 3
E (G, 3 √ (D,F) 6
H) (A,B) 8
(C,F) 3 √ (A,F) 10
(B,C) 4 √
Select first |V|–1 edges which do not
generate a cycle
3
F C edge d edge dv

A 3 v (B,E) 4 χ
4 (D,E) 1 √ (B,F) 4 χ
5
B D (D,G) 2 √ (B,H) 4 χ
H (E,G) 3 χ (A, 5 √
2 1
(C,D) 3 √ H)
3
G E (G,
H)
3 √ (D,F)
(A,B)
6
8
}
not
considere
d
(C,F) 3 √ (A,F) 10
Done
(B,C) 4 √

Total Cost = Σ dv = 21
Prim’s Algorithm

Select any vertex

B 5 A
C

3 Select the shortest


4 edge connected to
8 6
that vertex
8
AB 3
A D
7 F

5
4
2

E
Prim’s Algorithm

Select the shortest


edge connected to
B 5 any vertex already
C
connected.
3
4 AE 4
8 6

8
A D
7 F

5
4
2

E
Prim’s Algorithm

Select the shortest


edge connected to
B 5 any vertex already
C
connected.
3
4 ED 2
8 6

8
A D
7 F

5
4
2

E
Prim’s Algorithm

Select the shortest


edge connected to
B 5 any vertex already
C
connected.
3
4 DC 4
8 6

8
A D
7 F

5
4
2

E
Prim’s Algorithm

Select the shortest


edge connected to
B 5 any vertex already
C
connected.
3
4 EF 5
8 6

8
A D
7 F

5
4
2

E
Prim’s Algorithm

All vertices have been


connected.
B 5
C
The solution is
3
4 AB 3
8 6
AE 4
ED 2
8
DC 4
A D
7 F EF 5

5
4 Total weight of tree: 18
2

E
Some points to note

• Both algorithms will always give solutions with the same length.

• They will usually select edges in a different order – you must show
this in your workings.

• Occasionally they will use different edges – this may happen when
you have to choose between edges with the same length. In this
case there is more than one minimum connector for the network.

You might also like