Professional Documents
Culture Documents
Strukture Podataka I Algoritmi
Strukture Podataka I Algoritmi
Elektrotehnički fakultet
Milo V. Tomašević
Marko Mišić
Sadržaj
Algoritmi i strukture podataka
III Pretraživanje
IV Sortiranje
Uvod
Algoritmi i strukture podataka
➢ Osobine
✓ jedan ili više ulaza i izlaza
✓ nedvosmislenost i ostvarljivost naredbi
✓ konačno vreme izvršavanja
➢ Najčešće u pseudojeziku
Uvod
Zapis
Algoritmi i strukture podataka
➢ Koristi se u implementaciji
✓ dinamičkih struktura
✓ datoteka
Smeštanje u memoriji
a) kontinualna alokacija polja
b) vezivanje polja za početak reči
marka godina snaga
a) 14243 14243 1444442444443
1001444444444424444444444443
110 114
a1
112 116
10014444444444244444444443
a1
➢ Po relacijama elemenata
✓ linearne
✓ nelinearne
➢ Po mestu čuvanja
✓ unutrašnje (u operativnoj memoriji)
✓ spoljašnje (datoteke)
Sekvencijalna
➢ Fizički i logički poredak isti
➢ Pristup elementima direktan
➢ Obično za linearne strukture
a)
b)
Ulančana
➢ Fizički i logički poredak različiti
➢ Pristup elementima indirektan
➢ Koristi pokazivački mehanizam
➢ Obično za nelinearne strukture
➢ Operacije
✓ obilazak po poretku 1..n
✓ pretraživanje
✓ pristup elementu
✓ umetanje
✓ brisanje, ...
Linearne strukture podataka
Algoritmi i strukture podataka
➢ Implementacija
✓ sekvencijalna – niz
✓ ulančana – ulančana lista
➢ Disciplina pristupa
✓ LIFO - stek
✓ FIFO - red
Nizovi
Algoritmi i strukture podataka
➢ Selekcija elementa
➢ Operacije
Linearne strukture podataka
Smeštanje vektora
Algoritmi i strukture podataka
➢ Sekvencijalna reprezentacija,
ne razdvaja se logički koncept od implementacije
Al Ai
➢ Kontinualno
✓ prostorno efikasno
✓ neefikasan pristup
➢ Sa dopunjavanjem (padding)
✓ iskorišćenje prostora Su = s / s
✓ efikasniji pristup
➢ Pakovanje
✓ k = 1/s elemenata u jednoj reči
✓ prostorno iskorišćenje Su = ks
✓ Ai = Al + (i - l)/k
pozicija u reči (i - l) mod k
1 j N
1 1,1 1, j 1,N
X i i,1 i, j i,N
M M,1 M, j M,N
➢ Linearizacija
✓ po vrstama (row-major)
✓ po kolonama (column-major)
Linearne strukture podataka Nizovi Predstavljanje nizova u memoriji
Smeštanje po vrstama
Algoritmi i strukture podataka
➢ X[l1:u1, l2:u2]
1,1 1, j 1,N ... i,1 i,j i,N ... M,1 M,j M,N
➢ X[1:M,1:N]
➢ X[l1:u1, l2:u2]
1,1 i,1 M,1 ... 1,j i,j M,j ... 1,N i,N M,N
➢ X[1:M,1:N]
➢ Leksikogafski poredak
➢ Efikasno izračunavanje
offset = 0
for j = 1 to n do
offset = U [j ] offset + I [j ] - 1
end_for
A = A1,..,1 + s * offset
x x
x 0 x X
a) b)
X x
0 x
0 0
0 0 0 X
c) d)
X 0
0 0
1 2 n+1 1 n
1 1
2
X X
Y Y
n n
a) b)
➢ Vektorska reprezentacija
➢ Neefikasne operacije!
R C V
0 0 4 0 0 0 0 1 3 4
2 4 5
0 0 0 5 0 11 0
2 6 11
X = 0 0 0 0 0 0 0
4 1 9
9 0 0 8 0 0 0 4 4 8
0 0 0 0 0 0 15 5 7 15
➢ Po načinu povezanosti
✓ jednostruko ulančane
✓ dvostruko ulančane
✓ kružne
✓ nekružne
➢ Po organizaciji
✓ uređene
✓ neuređene
INSERT-AFTER(p, x)
q = GETNODE
info(q) = x
next(q) = next(p)
next(p) = q
y z y z
Þ
p p x
q q
y z x z
Þ
p p y
q q
Linearne strukture podataka Ulančane liste Operacije sa jednostruko ulančanim listama
Jednostruko ulančane liste
Algoritmi i strukture podataka
x y z
p
Þ
x z
DELETE(p)
q = next(p)
next(p) = next(q)
info(p) = info(q)
FREENODE(q)
x y y
Þ
p q p
SEARCH(list, x)
p = list
while (p nil) and (info(p) x) do
p = next (p)
end_while
return p
SEARCH-ORD(list, x)
p = list
while (p nil) and (info(p) < x) do
p = next(p)
end_while
if (p nil) and (info(p) > x) then
p = nil
end_if
return p
INSERT-AFTER-C(p, x)
q = GETNODE
info(q) = x
next(q) = next(p)
next(p) = q
if (list = p) then
list = q
end_if
list
list
Þ
y x
list
a) H
list
b) H
➢ next(prev(p)) = p = prev(next(p))
p q
prev info next
p q
prev info next
INSERT-AFTER-D(p, x)
q = GETNODE
info(q) = x
Umetanje r = next(p)
prev(q) = p
next(q) = r
next(p) = q
prev(r) = q
r
y z y z
Þ 3 5 6 4
p p
x
1 2
q q
DELETE-D(p)
q = prev(p)
Brisanje r = next(p)
next(q) = r
prev(r) = q
FREENODE(p)
x y z
q p r
Þ
x z
q r
Linearne strukture podataka Ulančane liste Operacije sa dvostruko ulančanim listama
Poređenje nizova i lista
Algoritmi i strukture podataka
➢ Ulančana reprezentacija
✓ obično bolje koristi prostor
✓ efikasnija za operacije umetanja, brisanja,
spajanja, ...
✓ fleksibilnost za dinamičke strukture
➢ Sekvencijalna reprezentacija
✓ efikasniji pristup elementu
✓ pogodna za statičke strukture
Linearne strukture podataka Ulančane liste Poređenje ulančane i sekvencijalne reprezentacije linearnih lista
Predstavljanje retkih nizova
Algoritmi i strukture podataka
4 1 9 4 4 8 5 7 15
R 1 2 3 4 5 6 7
C
1
1 3 4
2 4 5 2 6 11
4
4 1 9 4 4 8
5
5 7 15
➢ Vektorska realizacija
✓ prostorna neefikasnost
✓ efikasne operacije
(unija, presek, razlika, pripadnost)
➢ Ulančana implementacija
✓ čvor - element skupa
✓ prostorno efikasnije
✓ manje efikasne operacije
(za uređene liste – O(n),
za neuređene liste – O(n2))
a) A B C D E
b top
b) A B C D E
b top
c) A B F G
b top
Linearne strukture podataka Stekovi
Sekvencijalna reprezentacija
Algoritmi i strukture podataka
➢ Niz S[1:n]
6 5 9 1 6 5 9 1 3 2
top top
c)
6 5 9 1 3 2
top
Provera da li je Umetanje
prazan
STACK-EMPTY(S) PUSH(S, x)
if (top[S] = 0) then if (top[S] = n) then
return true ERROR(Overflow)
else else
return false top[S] = top[S] + 1
end_if S[top[S]] = x
end_if
Čitanje bez
Uklanjanje
uklanjanja
POP(S) TOP(S)
if (top[S] = 0) then if (top[S] = 0) then
return underflow return underflow
else else
x = S[top[S]] return S[top[S]]
top[S] = top[S] – 1 end_if
return x
end_if
S1 S2
a)
S1 S2
b)
S1 S2 S3 S4 S5
a)
S1 S2 S3 S4 S5
c)
Brisanje Umetanje
POP-M(i) PUSH-M(i, x)
if (top[i] = b[i]) then if (top[i ] = b[i + 1]) then
return underflow ERROR(Overflow)
else else
x = V[top[i]] top[i ] = top[i ] + 1
top[i] = top[i] – 1 V[top[i ]] = x
return x end_if
end_if
steka a) s
y
s
b) x y
PUSH-L(s, x) POP-L(s)
p = GETNODE if (s = nil) then
info(p) = x return underflow
next(p) = s else
s=p p=s
s = next(p)
x = info(p)
FREENODE(p)
return x
end_if
while (not(STACK-EMPTY(S))) do
x = POP(S)
OUTPUT(x, postfix)
rank = rank + R(x)
end_while
if (rank 1) then
ERROR(Nepravilan izraz)
end_if
a)
I H G F E D C B A
rear front
b)
I H G F E D C B A
rear front
➢Niz Q[1:n]
a) d) C
front
rear
b) A B e) C D E?
c) B C f) E?
➢Kružni bafer
Q[1] Q[n]
Q[n] Q[1]
front = rear = 0
rear
a) b)
front=rear front
front
c) d)
rear
front
rear
INSERT(Q, x)
rear[Q] = rear[Q] mod n + 1
if (front[Q] = rear[Q]) then
ERROR(Overflow)
Umetanje else
Q[rear[Q]] = x
if (front[Q] = 0) then
front[Q] = 1
end_if
end_if
Linearne strukture podataka Redovi Implementacija reda i operacije sa njim
Operacije sa redom
Algoritmi i strukture podataka
a) 2 0 3
front rear
b) 8 5 2 0 3 1
rear front
c) 8 5 2 0 3 1
rear front
DELETE(Q) QUEUE-EMPTY(Q)
if (front[Q] = 0) then if (front[Q] = 0) then
return underflow return true
else else
x = Q[front[Q]] return false
if (front[Q] = rear[Q]) then end_if
front[Q] = rear[Q] = 0
else
Provera
front[Q] = front[Q] mod n + 1
end_if
return x Brisanje
end_if
➢Niz Q[0:n-1]
front
front
Q[n-1] Q[0] rear
Provera Brisanje
QUEUE-EMPTY-0(Q) DELETE_0(Q)
if (front[Q] = rear[Q]) then if (front[Q] = rear[Q]) then
return true return underflow
else else
return false front[Q] = (front[Q] + 1) mod n
return Q[front[Q]]
end_if
end_if
Umetanje
INSERT_0(Q, x)
rear[Q] = (rear[Q] + 1) mod n
if (front[Q] = rear[Q]) then
ERROR(overflow)
else
Q[rear[Q]] = x
end_if
a)
b)
c)
q
a) y z v
rear
q
b) y z v x
rear
q
c) z v x
rear
INSERT-L(q, x) DELETE-L(q)
p = GETNODE if (q = nil) then
info(p) = x return underflow
next(p) = nil else
if (rear[q] = nil) then p=q
q=p x = info(p)
else q = next(p)
next(rear[q]) = p if (q = nil) then
end_if rear[q] = nil
rear[q] = p end_if
FREENODE(p)
return x
end_if
Linearne strukture podataka Redovi Implementacija reda i operacije sa njim
Prioritetni red
Algoritmi i strukture podataka
Uvod
Prioritetni red
Algoritmi i strukture podataka
PQ-INSERT(pq, x)
q = nil
p = pq
while (p nil) and (x info(p)) do
q=p
p = next(p)
end_while
if (q = nil) then
PUSH-L(pq, x)
else
INSERT-AFTER(q, x)
end_if