0% found this document useful (0 votes)
10 views26 pages

L3

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)
10 views26 pages

L3

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/ 26

‫ساختمان دادهها و الگوریتمها‬

‫فصل سوم‬

‫‪[email protected]‬‬ ‫‪۱۳۹۴‬‬ ‫صمد نجار‬


‫‪2‬‬

‫‪[email protected]‬‬

‫در جلسات قبل ‪...‬‬


‫مقدمهای بر ساختمان داده‬ ‫•‬
‫تعاریف اولیه‬ ‫•‬
‫مفاهیم پایه درباره پیچیدگی زمانی‬ ‫•‬
‫معرفی پیچیدگی زمانی و حافظهای‬ ‫•‬
‫نحوه پیدا کردن گام های برنامه‬ ‫•‬
‫نمادهای خاص پیچیدگی زمانی‬ ‫•‬
‫نحوه محاسبه انواع پیچیدگی ها و روشهای حل آن‬ ‫•‬
‫‪3‬‬

‫‪[email protected]‬‬

‫فهرست مطالب‬
‫‪‬مقدمهای بر الگوریتمها و مفاهیم پایه‬
‫‪‬معرفی پیچیدگی زمانی و حافظهای و روشهای تحلیل مسائل‬
‫‪‬معرفی ساختمان دادههای مقدماتی و الگوریتمهای وابسته به آنها‬
‫آرایه‬ ‫•‬
‫صف‬ ‫•‬
‫پشته‬ ‫•‬
‫لیست پیوندی‬ ‫•‬
‫‪‬تئوری درخت و گراف و الگوریتمهای مرتبط‬
‫‪‬الگوریتمهای مرتبسازی و تحلیل پیچیدگی مربوط به آنها‬
‫‪ ‬مباحث تکمیلی در ساختمان دادهها‬
‫‪4‬‬

‫‪[email protected]‬‬

‫تعاریف اولیه ‪...‬‬

‫• داده ایستا‪ :‬از فضای محدود و از پیشتعریف شده استفاده کرده و این فضا در طول برنامه ثابت‬
‫است‪.‬‬
‫• داده پویا‪ :‬با استفاده از اشاره گرها امکان تغییر نامحدود و پویا متناسب با دادهها را فراهم میکند‪.‬‬
‫• دادههای نیمه ایستا‪ :‬برخی از ساختمان داده ها را هم به صورت محدود با استفاده از آرایه‬
‫(ساختار ایستا) و هم به صورت نامحدود با استفاده از لیست پیوندی (ساختار پویا) پیادهسازی کرد‬
‫‪5‬‬

‫‪[email protected]‬‬

‫دسته بندي ساختمان دادهها‬


‫ساختمان داده‬

‫پویا‬ ‫نیمه ایستا‬ ‫ایستا‬

‫یرخ ی‬ ‫خ ی‬ ‫پشته‬ ‫صف‬ ‫یراولیه‬ ‫اولیه‬

‫درخت‬ ‫گراف‬ ‫لیست پیوندی‬ ‫رکورد‬ ‫رشته‬ ‫آرایه‬ ‫اشاره گر‬ ‫دد ا شاری‬ ‫دد صحی‬
‫‪6‬‬

‫‪[email protected]‬‬

‫دسته بندي ساختمان دادهها‬


‫ساختمان داده‬

‫پویا‬ ‫نیمه ایستا‬ ‫ایستا‬

‫یرخ ی‬ ‫خ ی‬ ‫پشته‬ ‫صف‬ ‫یراولیه‬ ‫اولیه‬

‫درخت‬ ‫گراف‬ ‫لیست پیوندی‬ ‫رکورد‬ ‫رشته‬ ‫آرایه‬ ‫اشاره گر‬ ‫دد ا شاری‬ ‫دد صحی‬
‫‪7‬‬

‫‪[email protected]‬‬

‫سوال‬

‫• آرایه چه نوع ساختاری است؟‬

‫• چرا از آرایه استفاده میکنیم؟‬

‫• نقاط قوت و ضعف این نوع ساختار داده چیست؟‬


‫‪8‬‬

‫‪[email protected]‬‬

‫‪Introduction Array‬‬

‫نوع داده مجرد یا انتزاعی (‪ :)ADT‬نوع دادهای است که در آن مشخصات دادهها و اعمال‬
‫بر روی آنها از بازنمایی و پیادهسازی داده جدا میشود‪.‬‬
9

Arrays [email protected]

Array:
a set of index and value

data structure:
For each index, there is a value associated with
that index.

Representation:
implemented by using consecutive memory.
10

Array (Cont.) [email protected]

Structure Array is
objects: A set of pairs <index, value> where for each value of index there is a value from the
set item. Index is a finite ordered set of one or more dimensions, for example, {0, … , n-1} for
one dimension, {(0,0),(0,1),(0,2),(1,0),(1,1),(1,2),(2,0),(2,1),(2,2)} for two dimensions, etc.
Functions:
for all A  Array, i  index, x  item, j, size  integer
Array Create(j, list) ::= return an array of j dimensions where list is a
j-tuple whose ith element is the size of the
ith dimension. Items are undefined.
Item Retrieve(A, i) ::= if (i  index) return the item associated with
index value i in array A
else return error
Array Store(A, i, x) ::= if (i in index)
return an array that is identical to array
A except the new pair <i, x> has been
inserted else return error
end array
11

Array (Cont.) [email protected]

int list[5], *plist[5];

list[5]: five integers


list[0], list[1], list[2], list[3], list[4]
*plist[5]: five pointers to integers
plist[0], plist[1], plist[2], plist[3], plist[4]

Implementation of 1-D array:


list[0] base address = 
list[1]  + sizeof(int)
list[2]  + 2*sizeof(int)
list[3]  + 3*sizeof(int)
list[4]  + 4*size(int)
12

Array (Cont.) [email protected]

Compare int *list1 and int list2[5] in C.

Same: list1 and list2 are pointers.


Difference: list2 reserves five locations.

Notations:
list2 is a pointer to list2[0]
(list2 + i) is a pointer to list2[i] (&list2[i])
*(list2 + i) is list2[i]
13

Array (Cont.) [email protected]

Example: 1-dimension array addressing


int one[] = {0, 1, 2, 3, 4};
Goal: print out address and value

void print1(int *ptr, int rows)


{
/* print out a one-dimensional array using a pointer */
int i;
printf)“Address Contents\n”(;
for (i=0; i < rows; i++)
printf)“%8u%5d\n”, ptr+i, *(ptr+i));
printf)“\n”(;
}
14

1-dimension array [email protected]

Float a[10];
‫‪15‬‬

‫)‪Array (Cont.‬‬ ‫‪[email protected]‬‬

‫مشخص کردن حافظه برای ناصر آرایه یک بعدی‪:‬‬

‫‪A: Array[L…U] of type‬‬

‫اگر ‪ type‬نوع عناصر آرایه به اندازه 𝑛 باشد و اگر فرض کنیم آدرس شروع آرایه 𝛼 باشد آنگاه‪:‬‬

‫تعداد عناصر آرایه ‪𝑈 − 𝐿 + 1 ==== A‬‬ ‫•‬

‫حافظه ی تخصیصی به این آرایه ===== 𝑛 × ‪𝑈 − 𝐿 + 1‬‬ ‫•‬

‫آدرس شروع عنصر ]‪𝑖 − 𝐿 × 𝑛 + 𝛼 ==== A[i‬‬ ‫•‬


‫‪16‬‬

‫‪[email protected]‬‬

‫‪Array example‬‬
‫آرایهی ‪ Test‬در زبان ‪ C‬به صورت زیر تعریف شده است‪ .‬اگر آدرس شروع این آرایه ‪2500‬‬
‫باشد‪ .‬م لوب است آدرس نصر ]‪ Test[30‬را بیابید‪.‬‬
‫;]‪float Test[200‬‬

‫‪Address (Test[30]) = & Test[30]= 30 − 0 × 4 + 2500 = 2620‬‬


17

2-dimension array [email protected]


‫‪18‬‬

‫‪2-dimension array‬‬ ‫‪[email protected]‬‬

‫مشخص کردن حافظه برای ناصر آرایه دو بعدی‪:‬‬


‫𝑒𝑝𝑦𝑡 𝑓𝑜 ‪𝐴: 𝐴𝑟𝑟𝑎𝑦 𝐿1 … 𝑈1 , 𝐿2 … 𝑈2‬‬

‫اگر ‪ type‬نوع ناصر آرایه (که هر نصر از این نوع به اندازه 𝑛 است) باشد و اگر فرض کنیم آدرس شروع این آرایه 𝛼 باشد آنگاه‪:‬‬

‫تعداد ناصر آرایه ‪(𝑈1 −𝐿1 + 1) × (𝑈2 − 𝐿2 + 1) ==== A‬‬ ‫•‬

‫حافظه ی تخصیصی به این آرایه === 𝑛 × )‪(𝑈1 −𝐿1 + 1) × (𝑈2 − 𝐿2 + 1‬‬ ‫•‬

‫‪𝑖 − 𝐿1 × 𝑈2 − 𝐿2 + 1 + 𝑗 − 𝐿2‬‬ ‫آدرس شروع نصر ]‪ A[i,j‬در حالتی که آرایه س ری ذخیره شده است = 𝛼 ‪× 𝑛 +‬‬ ‫•‬

‫‪𝑗 − 𝐿2 × 𝑈1 − 𝐿1 + 1 + 𝑖 − 𝐿1‬‬ ‫آدرس شروع نصر ]‪ A[i,j‬در حالتی که آرایه ستونی ذخیره شده است = 𝛼 ‪× 𝑛 +‬‬ ‫•‬
19

[email protected]

‫ماتریس اسپارس‬
20

[email protected]

‫ماتریس اسپارس‬
21

[email protected]

‫ماتریس اسپارس‬
22

[email protected]

‫ماتریس اسپارس‬
23

[email protected]

‫ماتریس اسپارس‬
‫‪24‬‬

‫‪[email protected]‬‬

‫‪Home Work 4‬‬


‫• برنامه ای بنویسید که ماتریس ‪ )n*n( A‬را که یک ماتریس سپارس است بگیرد و به صورت که حافظه کمی‬
‫استفاده نمایید ذخیره کنید‪( .‬حداکثر مقدار ‪ 50 n‬میباشد‪ ،‬سعی شود برای ماتریس اولیه که از ورودی گرفته‬
‫میشود حافظه تخصیص داده نشود)‬
‫‪25‬‬

‫‪[email protected]‬‬

‫دسته بندي ساختمان دادهها‬

‫ساختمان داده‬

‫پویا‬ ‫نیمه ایستا‬ ‫ایستا‬

‫یرخ ی‬ ‫خ ی‬ ‫پشته‬ ‫صف‬ ‫یراولیه‬ ‫اولیه‬

‫درخت‬ ‫گراف‬ ‫لیست پیوندی‬ ‫ر ورد‬ ‫رشته‬ ‫آرایه‬ ‫اشاره گر‬ ‫دد ا شاری‬ ‫دد صحی‬
26

Structures (records) [email protected]

‫ مجمو های از ناصر هست که‬:‫• ساختار‬


struct {
.‫لزومی ندارد دادههای آن یکسان باشد‬
char name[10];
int age;
float salary;
} person;

strcpy)person.name, “james”(;
person.age=10;
person.salary=35000;

You might also like