0% found this document useful (0 votes)
12 views25 pages

Doublylinkedlist Cours

cour ASD3

Uploaded by

kefifyoucef2020
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views25 pages

Doublylinkedlist Cours

cour ASD3

Uploaded by

kefifyoucef2020
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

PEOPLE’S DEMOCRATIC REPUBLIC OF ALGERIA

Ministry of Higher Education and Scientific Research

Algorithmics and data structures ADS3

Presented by Mr.Mahseur Mohammed


CONTENTS

1 Complexity of algorithms 1
1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Definition of complexity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.3 Types of complexity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3.1 Spatial complexity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3.2 Time complexity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.4 Abstract . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.5 Complexity calculation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.5.1 Complexity of a sequence of instructions . . . . . . . . . . . . . . . . . . 4
1.5.2 Complexity of a conditional statement . . . . . . . . . . . . . . . . . . . 5
1.5.3 Complexity of a loop for . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.5.4 Complexity of a loop while . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.5.5 Complexity of a loop do while . . . . . . . . . . . . . . . . . . . . . . . . 6
1.5.6 Complexity of calling a function or procedure . . . . . . . . . . . . . . . 7
1.5.7 Complexity of a recursive function . . . . . . . . . . . . . . . . . . . . . 7
1.6 Asymptotic complexity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.6.1 Landau concept O(nf (N )) . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.6.2 Simplification of complexity . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.6.3 Main orders of magnitude . . . . . . . . . . . . . . . . . . . . . . . . . . 9

2 Advanced dynamic data structures 14


2.1 Doubly linked list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.1.1 Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.1.2 Data structure declaration . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.1.3 Manipulation of doubly linked lists . . . . . . . . . . . . . . . . . . . . . 15

I
LIST OF FIGURES

1.1 Types of complexity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2


1.2 Time complexity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 Complexity in summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

II
LIST OF ALGORITHMS

1 Example1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2 Example2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3 Maximum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
4 Prime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
5 TheSum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
6 Money . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
7 Sorting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
8 MatrixFrequency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
9 Rest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
10 The Factorial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
11 Fibonacci Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

III
CHAPTER 1
COMPLEXITY OF ALGORITHMS

1.1 Introduction
▷ An algorithm is a method of solving a problem of size N

▷ For a given problem, there may be several different algorithms.

▷ This can open our eyes to a comparison of algorithms, which one is more efficient when
N is very large

▷ For small problems, the comparison is not interesting

▷ Measure the efficiency of an algorithm→ calculate its complexity

1.2 Definition of complexity


• It is a criterion for measuring an algorithm

• It is the quantity of resources (space, time) consumed (mobilized) by the implemented


program

1
CHAPTER 1. COMPLEXITY OF ALGORITHMS

1.3 Types of complexity

Figure 1.1: Types of complexity

1.3.1 Spatial complexity


• It is linked to the size of the memory space occupied by the program during its execution.

• At the beginning of the computer age, this parameter had about the same importance as
time complexity

• In the 70s for example, the size of a central memory (MC) of a big machine = 128 KB

• Today, the MC capacity of a PC is worth a few gigabytes

• So, the worry of optimizing the special complexity has disappeared, all that remains is to
study the temporal complexity

1.3.2 Time complexity


• It describes the time required to execute an algorithm (or program)

• We therefore talk about the practical execution time of a program and the theoretical
execution time of an algorithm

Figure 1.2: Time complexity

2
CHAPTER 1. COMPLEXITY OF ALGORITHMS

Practical execution time

It depends on several factors such as:

• The machine (Processor, RAM, etc.)

• The operating system

• The programming language (Assembler, C, Java, etc.)

• The nature of the data handled

• The algorithm adopted

Several factors intervene, which is why the study of complexity was limited to the theoretical
execution time of the algorithm

Theoretical execution time

• To solve a problem, the algorithm must execute a set of instructions (commands)

• Each instruction is composed of one or more elementary operations

• An elementary operation is a fundamental operation with zero, one or two operands


(generally) such as assignment, arithmetic, logic, input, output, etc.

• Example instructions:
X←a; (an assignment operation)
X←a+b; (two operations: addition and assignment)
X← a+b+c; (3 operations: 2 additions and 1 assignment)

• Suppose that the average execution time of an operation is t, and the number of elemen-
tary operations of an algorithm is Nb

• The theoretical execution time is: Nb × t

• Knowing that the average execution time of an elementary operation is a constant linked
to several factors (machine)

• Therefore, the study of the theoretical execution time was limited to the number of
elementary operations carried out

Worst-case complexity

• When solving a given problem, we can find three cases: The best case; The average case
and the worst case

• Example: algorithm for finding an existing value in a vector

3
CHAPTER 1. COMPLEXITY OF ALGORITHMS

– The best case is that the value is in the first box


– The average case is that the value lies in the middle of the vector
– The worst case is that the value is in the last box

• The study of complexity was limited to the worst case

1.4 Abstract

Figure 1.3: Complexity in summary

Studying complexity means calculating the number of elementary operations necessary to solve
a problem in the worst case based on its size, .

1.5 Complexity calculation


There is no systematic methodology for counting the elementary operations of any algorithm.
However, rules are commonly accepted and used by the computer science community.

1.5.1 Complexity of a sequence of instructions


Let S be a sequence of k instructions:

Begin
instruction 1
instruction 2
.
.
instruction k
End

Let N bi be the number of elementary operations of instruction (i):

4
CHAPTER 1. COMPLEXITY OF ALGORITHMS

k
Cs =
X
N bi
i=1

1.5.2 Complexity of a conditional statement


Let a conditional statement:

If (expression) then
Block1
Else
Block2
End if

Let N bexpression : the number of elementary operations to evaluate the expression of the
condition
Let N bbloc1 and N bbloc2 : the number of elementary operations of block 1 and 2 respectively
It is impossible to determine which part of the instruction will be executed, so we take an upper
bound:

Ccond = N bexpression + max(N bbloc1 , N bbloc2 )

Example:
1: Begin
2: if (A > B − 1) then
3: A←A−B+1
4: else
5: A ← (A + B) ∗ (B − 1)
6: end if
7: End.
N bexpression = 2; M ax(N bbloc1 , N bbloc2 ) = M ax(3, 4) = 4; Ccond = 2 + 4 = 6

1.5.3 Complexity of a loop for


Consider a bounded iteration:

For i := Bmin to Bmax do


Block
End for

Let N bheader : the number of elementary operations in the loop header


Let N bblock : the number of elementary operations of the block of instructions of the loop

Cf or = (Bmax + 1 − Bmin) × (N bheader + N bblock ) + N bheader

Example:

5
CHAPTER 1. COMPLEXITY OF ALGORITHMS

1: Begin
2: for i = 1 to N do
3: if (A > B − 1) then
4: A←A−B+1
5: else
6: A ← (A + B) ∗ (B − 1)
7: end if
8: end for
9: End.
Bmax = N ; Bmin = 1; N bheader = 2; N bblock = 6
Cf or = (N + 1 − 1) × (2 + 6) + 2 = 8N + 2

1.5.4 Complexity of a loop while


Consider an unbounded iteration:

While (Cond) do
Block
End while

Let N bCond : the number of elementary operations of the evaluation of the condition
Let N bblock : the number of elementary operations of the loop block
Let N biterations : the number of repetitions, it is evaluated inductively in the worst case

Cwhile = (N biterations ) × (N bCond + N bblock ) + N bcond

Example:
1: Begin
2: while (N ! = 0) do
3: if (A > B − 1) then
4: A←A−B+1
5: else
6: A ← (A + B) ∗ (B − 1)
7: end if
8: N ← N/2
9: end while
10: End.
N bCond = 1; N bblock = 8; N biterations = Ln(N )/Ln(2);
Cwhile = (Ln(N )/Ln(2)) × (1 + 8) + 1 = 9Ln(N )/Ln(2) + 1

1.5.5 Complexity of a loop do while


Consider an unbounded iteration:

6
CHAPTER 1. COMPLEXITY OF ALGORITHMS

do
Block
while (cond)

Cdowhile = (N biterations ) × (N bCond + N bblock )

• But it is not easy to find the number of iterations in a While loop (in a do while loop too)

• If we notice a small value (i) tends towards a large value (N) by the addition of a step,
or the opposite by the subtraction of a step: then the number of iterations = N/Step

• If we notice a small value (i) tends towards a large value (N) by multiplication by one step,
or the opposite by division by one step: then the number of iterations=Ln(N)/Ln(Step)

1.5.6 Complexity of calling a function or procedure


The cost of calling a procedure (or a function)=The cost of the procedure body for its param-
eters + The cost of evaluating its parameters Example:

function Even(N:integer):boolean
if (N mod 2=0) then
return true;
else
return false;
end if
end function

Algorithm 1 Example1
1: Var A: integer
2: Begin
3: Read(A);
4: if (Even(A)=true) then
5: A←A+1
6: else
7: A←A+2
8: end if
9: End.

Cf unction = 3
Calgo = 1 + 1 + 3 + 1 + 2 = 8

1.5.7 Complexity of a recursive function


in this case we must calculate the complexity of the function as a function of the parameter N
which represents the size of the problem.
Example:

7
CHAPTER 1. COMPLEXITY OF ALGORITHMS

function Power(A, N:integer):integer


if (N =0) then .............(1)
return 1;
else
return (A*Power(A,N-1));..........(1)+(1)+(1)+(2)+CN −1
end if
end function

The complexity of the function CN = CN −1 +6= C0 + N ∗ 6 = 6 ∗ N + 2

Algorithm 2 Example2
1: Var A,N: integer
2: Begin
3: Read(A,N); .............(2)
4: Write("The Power=", Power(A,N));.........(1)+(1)+(2)+CN
5: End.

The complexity of the algorithm =6N+8

1.6 Asymptotic complexity


Asymptotic complexity consists of studying the approximate behavior of the number of ele-
mentary operations of the algorithm when the size of the problem treated is sufficiently large
(tends towards infinity)

1.6.1 Landau concept O(nf (N ))


This notion characterizes the asymptotic behavior of complexity when N → ∞ , (N = the
size of the problem) Let C(n): the number of operations as a function of N We say that:
C(n) = O(f (n)) if:
∃c, ∃n0 such that : ∀n > n0 , C(n) ≤ c × f (n)
We say that C(n) is of the order of magnitude of O(f(n))

8
CHAPTER 1. COMPLEXITY OF ALGORITHMS

1.6.2 Simplification of complexity


Two rules to apply:

• We only keep the dominant term (larger when n → ∞)

• We remove the multiplicative constants

Example: C(n) = 3n5 − 2n3 + 5n2 + 7


The dominant term = 3n5 ; After removing the multiplicative constants: C(n) = n5 ;
We write C(n) = O(n5 )

1.6.3 Main orders of magnitude

9
CHAPTER 1. COMPLEXITY OF ALGORITHMS

Module: ASD3
Teacher : Mahseur Mohammed

Series No. 1 - Complexity

Calculate the complexity and order of magnitude of the following algorithms:

Algorithm 3 Maximum
1: Var A, B, C, Max: integers;
2: Begin
3: Read(A,B,C);
4: if (A>B) then
5: if (A>C) then
6: Max ← A;
7: else
8: Max ← C;
9: end if
10: else
11: if (B>C) then
12: Max ← B;
13: else
14: Max ← C;
15: end if
16: end if
17: Write(Max);
18: End.

Algorithm 4 Prime
1: Var N, i, cpt: integers;
2: Begin
3: Read(N);
4: if (N=1) then
5: Write(N,"is not a prime number");
6: else
7: i ← 2; cpt ← 0;
8: while (i≤ N/2)And(cpt=0) do
9: if (N mod i=0) then
10: cpt ← cpt+1;
11: else
12: i ← i+1;
13: end if
14: end while
15: if (cpt=0) then
16: Write(N,"is a prime number");
17: else
18: Write(N,"is not a prime number");
19: end if
20: end if
21: End.

10
CHAPTER 1. COMPLEXITY OF ALGORITHMS

Algorithm 5 TheSum
1: Var N, p, i, fact, puiss: integer; F: real;
2: Begin
3: Read(N,p);
4: F ← 1; puiss ← 1; fact ← 1;
5: for i= 1 to N do
6: puiss ← puiss*p;
7: fact ← fact*i;
8: F ← F+puiss/fact;
9: end for
10: Write(F);
11: End.

Algorithm 6 Money
1: Var N, a, b, c, d: integers
2: Begin
3: Read(N);
4: for a= 0 to N do
5: for b= 0 to N div 10 do
6: for c= 0 to N div 20 do
7: for d= 0 to N div 50 do
8: if (N=a +10*b+20*c+50*d) then
9: Write(a,b,c,d);
10: end if
11: end for
12: end for
13: end for
14: end for
15: End.

Algorithm 7 Sorting
1: Var i, j, sauv: integers;
2: T: Array [1..100] of integers;
3: Begin
4: Read(N);
5: for i= 1 to N-1 do
6: for j= i+1 to N do
7: if (T[i]<T[j]) then
8: sauv←T[i];
9: T[i]←T[j];
10: T[j]←sauv;
11: end if
12: end for
13: end for
14: End.

11
CHAPTER 1. COMPLEXITY OF ALGORITHMS

procedure ReadingMatrix(E/: N:integer; S/: M: Array[1..1000;1..1000]of integers)


Var i, j: integers;
for i= 1 to N do
for j= 1 to N do
Read(M[i,j]);
end for
end for
end procedure

function frequency(N,V:integers; M: Array[1..1000; 1..1000] of integers):integer


Var i, j, Cpt:integers;
Cpt←0;
for i= 1 to N do
for j= 1 to N do
if (M[i,j]=V) then
Cpt←Cpt+1;
end if
end for
end for
Return Cpt;
end function

Algorithm 8 MatrixFrequency
1: Var M: Array[1..1000; 1..1000] of integer ; N, V:integer;
2: Begin
3: Read(N); Read(V); Cpt←0;
4: ReadingMatrix(N,M);
5: Write(Frequency(N,V,M));
6: End.

Algorithm 9 Rest
1: Var N, d, r:integer;
2: Begin
3: Read(N); r←N;
4: while (r>d) do
5: r←r-d;
6: end while
7: Write(r);
8: End.

12
CHAPTER 1. COMPLEXITY OF ALGORITHMS

function Factorial(N:integer):integer
if (N=0) then
return 1;
else
return N*Factorial(N-1);
end if
end function

Algorithm 10 The Factorial


1: Var N: integer;
2: Begin
3: Read(N);
4: Write(Factorial(N));
5: End.

function Fibonacci(N:integer):integer
if (N<2) then
return N;
else
return Fibonacci(N-1)+Fibonacci(N-2);
end if
end function

Algorithm 11 Fibonacci Sequence


1: Var N: integer;
2: Begin
3: Read(N);
4: Write(Fibonacci(N));
5: Fin.

13
CHAPTER 2
ADVANCED DYNAMIC DATA STRUCTURES

2.1 Doubly linked list


A doubly linked list is a more complex data structure than a singly linked list, but it offers
several advantages. The main advantage of a doubly linked list is that it allows for efficient
traversal of the list in both directions. This is because each node in the list contains a pointer
to the previous node and a pointer to the next node. This allows for quick and easy insertion
and deletion of nodes from the list, as well as efficient traversal of the list in both directions.

2.1.1 Definition
A doubly linked list is a data structure that consists of a set of nodes, each of which contains
a value and two pointers, one pointing to the previous node in the list and one pointing to the
next node in the list. This allows for efficient traversal of the list in both directions, making it
suitable for applications where frequent insertions and deletions are required.

14
CHAPTER 2. ADVANCED DYNAMIC DATA STRUCTURES

2.1.2 Data structure declaration


Type Node=Record
Data: any type;
Next, Prev : Ptr Node;
End;
Type DoublyLL=Record
Head, Tail: Ptr Node;
End;

2.1.3 Manipulation of doubly linked lists


initialize a doubly linked list

Calling this procedure is necessary when creating the list

procedure Init(O/L:DoublyLL)
L.Head ←NULL;
L.Tail ←NULL;
end procedure

void Init(DoublyLL *PL){(*PL).Head=NULL;(*PL).Tail=NULL;}

Insertion in a Doubly Linked List

Insertion at the Beginning in Doubly Linked List: To insert a new node at the front
of doubly linked list,

• Create a new node, say NewNode with its previous pointer as NULL.

• Set the next pointer to the current head, NewNode->next ← head.

• Check if the linked list is not empty then we update the previous pointer of the current
head to the new node, head->prev ← NewNode, else Tail←NewNode;

• Finally, we return the new node as the head of the linked list: Head←NewNode.

15
CHAPTER 2. ADVANCED DYNAMIC DATA STRUCTURES

procedure InsertBegin(IO/L:DoublyLL, I/Val:integer)


Var NewNode: ptr Node;
Allocate(NewNode);
if (NewNode=NULL) then
Write("allocation problem");
else
NewNode->Data ←Val; NewNode->Prev ←NULL;NewNode->Next←L.Head;
if (L.Head=NULL) then
L.Tail←NewNode;
else(L.Head)->Prev←NewNode;
end if
L.Head←NewNode;
end if
end procedure

Insertion at the end in Doubly Linked List: To insert a new node at the end,

• Allocate memory for a new node, say NewNode and assign the provided value to its data
field.

• Initialize the next pointer of the new node to NULL, NewNode->next ← NULL.

• Set the prev pointer of new node to last node, NewNode->prev ← Tail

• If the linked list is empty, we set the new node as the head of linked list Head←NewNode,
else Tail->Next←NewNode

• Set the Tail←NewNode

procedure InsertEnd(IO/L:DoublyLL, I/Val:integer)


Var NewNode: ptr Node;
Allocate(NewNode);
if (NewNode=NULL) then
Write("allocation problem");
else
NewNode->Data ←Val; NewNode->Prev ←L.Tail;NewNode->Next←NULL;
if (L.Tail=NULL) then
L.Head←NewNode;
else(L.Tail)->Next←NewNode;
end if
L.Tail←NewNode
end if
end procedure

16
CHAPTER 2. ADVANCED DYNAMIC DATA STRUCTURES

Creating a doubly linked list with insertion at the end

procedure Creation(O/L:DoublyLL)
var Answer: char; Val:int
Init(L);
repeat
Write("give a value: ");Read(Val);InsertEnd(L,Val);
Write("other values:");Read(Answer);
until (Answer=’n’ or Answer=’N’);
end procedure

Traversal in Doubly Linked List

To Traverse the doubly list, we can use the following steps:

a. Forward Traversal

• Initialize a pointer to the head of the linked list.

• While the pointer is not null:

– Visit the data at the current node.


– Move the pointer to the next node.

Example: displaying a list

procedure ListDisplay(I/L:DoublyLL)
Var P: ptr Node;
P ←L.Head;
while (P!=NULL) do
write(P->Data); P←P->Next;
end while
end procedure

b. Backward Traversal

• Initialize a pointer to the tail of the linked list.

• While the pointer is not null:

– Visit the data at the current node.


– Move the pointer to the previous node.

17
CHAPTER 2. ADVANCED DYNAMIC DATA STRUCTURES

Deletion at a Specific Position in Doubly Linked List

To delete a node at a specific position in doubly linked list, we can use the following steps:

• Perform a search for the node containing a given value V

• If the value does not exist in the list then display a non-existence message.

• Otherwise then the value V exists in the node whose address is P

– If P is not the head of the linked list, update the next pointer of the node before P
to point to the node after P: (P->Prev)->Next = P->Next.
Otherwise update the head: L.Head=P->Next.
– If P is not the last node of the linked list, update the previous pointer of the node
after P to the node before P: (P->Next)->Prev = P->prev.
Otherwise update the Tail: L.Tail=P->Prev.

• Free the memory allocated for the deleted node.

18
CHAPTER 2. ADVANCED DYNAMIC DATA STRUCTURES

procedure Deletion(IO/L:DoublyLL, I/V:integer)


Var P: ptr Node; Find: Boolean;
P ←L.Head;Find ←False;
while (P!=NULL and Not Find) do
if (P->Data=V) then
Find←True
else
P←P->Next;
end if
end while
if (!Find) then
Write("value does not exist!");
else
if (P->Prev=NULL) then
L.Head=P->Next;
else
(P->Prev)->Next←P->Next;
end if
if (P->Next=NULL) then
L.Tail←P->Prev;
else
(P->Next)->Prev←P->Prev;
end if
free(P)
end if
end procedure

Insertion after a given node in Doubly Linked List

To insert a new node after a specific node:

• Find the given node in the linked list, say P.

• Once we find it, create a new node say NewNode with the given input data.

• Set the new node’s previous pointer to given node, NewNode->prev = P.

• Set the new node’s next pointer to the given node’s next, NewNode->next = P->next.

• Then, we update the next pointer of given node with new node, P->next = NewNode.

• Also, if the new node is not the last node of the linked list, then update previous pointer
of new node’s next node to new node, (NewNode->next)->prev = NewNode.
Otherwise update the Tail: L.Tail=NewNode

19
CHAPTER 2. ADVANCED DYNAMIC DATA STRUCTURES

procedure InsertAfter(IO/L:DoublyLL, V1:integer, V2:integer)


Var NewNode,P: ptr Node; Find:boolean;
P ←L.Head; Find←False;
while (P!=NULL and Not Find) do
if (P->Data=V1) then
Find←True
else
P←P->Next;
end if
end while
if (!Find) then
Write("value does not exist!");
else
Allocate(NewNode);
if (NewNode=NULL) then
Write("alocation problem");
else
NewNode->Data←V2;NewNode->Prev←P;
NewNode->Next←P->Next;P->Next←NewNode;
if (P=L.Tail) then
L.Tail←NewNode;
else
(NewNode->Next)->Prev←NewNode;
end if
end if
end if
end procedure

Here is an example of a program in C language that uses all of these features.

20
CHAPTER 2. ADVANCED DYNAMIC DATA STRUCTURES

21

You might also like