Docsity

Data Structures and Algorithms: Elementary Concepts and Implementations - Prof. Juan Cebra, Study notes of Computer Science

Partial preview of the text

Download Data Structures and Algorithms: Elementary Concepts and Implementations - Prof. Juan Cebra and more Study notes Computer Science in PDF only on Docsity!

Data Structures and Algorithms

Elementary data structures

Computational geometry

Scientific codes

Scientific codes are usually designed to solve largecomplex problems representing natural phenomena

This requires: •^

Code modularization & maintenance

-^

Logical organization of the data

-^

Fast procedures to solve the problem

Example Makefile

EXE = run OBJ = main.o file.o

calc.o

LIB

= -llapack –lblas –lm –lgfortran

CMP = gcc –g –c --pedantic LNK = gcc $(EXE): $(OBJ)

$(LNK) –o $@ $(OBJ) $(LIB)

clean:

rm –f *.o

%.o: %.c

$(CMP) $?

Speed

The speed of a code is affected by: •^

Memory access^ – Cache vs main memory: contiguous data^ – Graphic memory vs main memory^ – Virtual memory: the end of the game

-^

Operation count^ – The total number of operations^ – Integer vs floating point operations^ – Sum / multiplications / divisions / square roots / sin-cos-exp

-^

File I/O^ – Ascii vs binary

-^

Communications^ – Shared memory vs distributed memory

Data structures

•^

Data structure are containers for storing data into thecomputer’s memory in an organized manner

-^

Data structures allow logical and efficient archival andretrieval of data items

Algorithms

•^

Algorithms are procedures to perform a specific task

-^

Data structures usually have associated algorithms forefficient access to the data stored in the data structures

Elementary data structures

•^

Arrays

-^

Linked lists

-^

Stacks

-^

Queues

-^

Hashes

Arrays

•^

An array is a fixed number of data items that are storedcontiguously and that are accessible by an index

-^

Arrays are defined as primitive in most programminglanguages (C, C++, Fortran, …)

-^

Arrays can be statically (size given when declaring thevariable) or dynamically allocated (malloc…)

-^

We refer to the ith element of an array a as: C/C++:

a[i]

Fortran:

a(i)

a

0

1

2

3

4

5

6

7

double a[100];double ptr;ptr=a;ptr=&a[0];ptr=malloc(100sizeof(double));ptr+=10;ptr=&a[10];a[10]=5.0;(ptr+10)=5.0;ptr[10]=5.0;*

Pointers in C/C++

•^

A pointer is a variable that contains a memory location

-^

A pointer can be made to point to different positions inthe computer’s memory

-^

Through a pointer one can access the data stored at thelocation pointed to by the pointer

(^0) a

1

2

3

4

5

6

7

ptr

Casting Pointers

•^

The pointer arithmetics takes into account the type ofdata being pointed to (jumps in steps of sizeof(*ptr) )

-^

Pointers can be ‘cast’ to access the memory in a different manner: float

*fptr;

char

*cptr;

cptr=(char)*

dptr;

0

1

2

3

4

5

6

7

fptr

float 1

float 2

Linked Lists

•^

Linked lists are defined as a primitive in some languages(Lisp, Logo) but not in most commonly used languages

-^

Main advantages of linked lists:^ – Dynamic data structure: can grow and shrink in size^ – Flexible in allowing items to be rearranged efficiently

-^

Disadvantages:^ – Slower than arrays^ – Need to traverse the list to find an item

Linked lists: basic operations

•^

Linked list with dummy nodes

-^

Adding an item

-^

Deleting an item

A^

B^

C^

D^

E

head

tail

A^

B^

C^

D^

E

head

tail

X

Delete link:

A^

B^

C^

D^

E

head

tail

X

Add links:

A^

B^

C^

D^

E

head

tail

X

Linked lists: pointer implementation (C)

Deletion operation: *void deleteNext(struct node n) {

n->next=n->next->next; } Insertion operation: **nodeT insertAfter(int item,struct node n) {

nodeT x=(nodeT)malloc(sizeof(x)); x->item=item; x->next=n->next; n->next=x; return x; }*

A^

B^

C^

D^

E

head

tail

X

A^

B^

C^

D^

E

head

tail

X

A^

B^

C^

D^

E

head

tail

X

Linked lists: array implementation (C)

Data structure: int

item[max+2],

next[max+2];

//

parallel

arrays

int

head=0,

tail=1,

x=2;

//

x:

next

unused

position

Initialization function: void

init()

{

next[head]=tail;

next[tail]=tail;

}