Presentation is loading. Please wait.

Presentation is loading. Please wait.

بردارها و ماتريس ها ساختمان داده ها

Similar presentations


Presentation on theme: "بردارها و ماتريس ها ساختمان داده ها"— Presentation transcript:

1 بردارها و ماتريس ها ساختمان داده ها
برگرفته از :

2 ليست خطي مجموعه پويا ساختاري براي نگهداري چندين شي هم نوع است
ليست خطي براي نگهداري مجموعه هاي پوياي تك بعدي استفاده مي شود ( بردارها،...) class Linear List { int list-size Object Type Members[list-size] //Operations AddNewMember (Object Type m) ; RemoveMember(int n) ; }

3 نمايش ليست خطي با آرايه L = (a, b, c, d, e)
ليست با آرايه يك بعدي مانند elements نشان داده مي شود: 1 2 3 4 5 6 a b c d e All array representations of a linear list use an array (say one-dimensional). Element.length = 15. Different array representations result when different mappings are used between list elements and array positions. The most intuitive and simple mapping is to map list element I into array position I. L = (a, b, c, d, e) عضو i ام آرايه در محل elements[i] قرار مي گيرد

4 نگاشت راست به چپ a b c d e A different mapping of linear list elements into a one-dimensional array.

5 نگاشت يك در ميان a b c d e Yet another mapping.

6 نگاشت چرخشي a b c d e And if that isn’t enough, here is yet another mapping.

7 نمايش معمول size = 5 عضو i ام آرايه در محل elements[i] قرار مي گيرد
1 2 3 4 5 6 a b c d e size = 5 عضو i ام آرايه در محل elements[i] قرار مي گيرد از متغير size‌براي نگهداري تعداد اعضاي ليست استفاده مي شود This is the representation used in the text. When this mapping is used, list operations such as isEmpty(), size(), get(index) Become easy to implement.

8 Add/Remove An Element size = 5 add(1,g) size = 6 a b c d e a g b c d e
To add an element we must physically relocate elements that are to be to the right of the newly inserted element. We must also increment size by 1. The slide shows add(1,g). remove(1) is the reverse. To remove an element we must move elements to the right of the removed element left by 1 and reduce size by 1. a g b c d e size = 6

9 پياده سازي ليست نوع اعضاي ليست در طراحي كلي، معلوم نيست
در جاوا هر كلاسي از كلاس Object ارث مي برد اعضاي ليست را از نوع Object ‌تعريف كنيد در اين صورت نوع داده هاي اوليه ( int, char, float ,…) را نمي توان در ليست قرار داد Wrapper Classes و کاربرد آنها To use this representation in Java, we must know/provide a data type and length of the array element[]. Wrapper types Integer, Float, Double, etc provided in Java.

10 مديريت اندازه ليست هنگام ساختن ليست از تعداد قطعي اعضاي آن خبر نداريم
بايد آنقدر ليست را بزرگ بسازيم كه براي هر تعداد عضو، گنجايش داشته باشد

11 ابتدا آرايه بزرگي مي سازيم
افزودن طول آرايه طول اين آرايه 6 است a b c d e f ابتدا آرايه بزرگي مي سازيم newArray = new Object[15];

12 افزودن طول آرايه سپس اعضاي آرايه اول را در آن كپي مي كنيم a b c d e f

13 افزودن طول آرايه element.length = 15
در نهايت نام آرايه را تعويض مي كنيم element = newArray; a b c d e f element[0] element.length = 15

14 افزودن طول آرايه، پياده سازي جاوا
// create a new array of proper length and data type Object [] newArray = new Object [newLength]; // copy all elements from old array into new one System.arraycopy(element, 0, newArray, 0, element.length); arraycopy(sourceArray, sourceStart, destArray, destStart, numberOfElementsToCopy) When writing a general method to change the length of an array whose data type is any subclass of Object, we need to use the more complex new array creation statement used on page 160 of the text. // rename array element = newArray;

15 طول آرايه جديد چقدر بايد باشد؟
حداقل يكي بيشتر از آرايه فعلي! هزينه افزايش طول آرايه برابر است با theta(M) ‌ كه در آن M طول افزوده شده است هزينه n بار افزايش طول يك آرايه خالي از مرتبه theta(n2) است When array length is increased by 1 each time we need to resize the array element[], the cost of n add operations goes up by Theta(n2). To see this suppose we start with an empty list and element.length = 1. Each element, other than the first, that is added to the list requires an array resize to be done. When element i, i > 0 is added, the array resizing requires i steps. So the total array resizing cost is Theta(n2). If the add operations are all done at the right end, then the cost of all n adds (in the absence of array resizing) is Theta(n). The resizing time dominates the overall effort. When the additions are done at the front of the list the resizing time is of the same order as the add time.

16 space needed = 2 * newLength – 1
حافظه مورد نياز element[6] a b c d e f newArray = new char[7]; space needed = 2 * newLength – 1

17 طول جديد دوبرابر طول قبلي
Double the array length. a b c d e f newArray = new char[12]; a b c d e f هزينه افزودن n عضو جديد از مرتبه theta(n) ‌است حافظه مورد نياز = 1.5 * طول جديد = 3* طول قبلي

18 طول آرايه جديد چقدر بايد باشد؟
اگر طول جديد مضربي از طول فعلي باشد: new length = c * old length افزودن n عضو هزينه اي از مرتبه Theta(n) خواهد داشت اگر طول جديد طول فعلي + مقدار ثابتي باشد: new length = c + old length افزودن n عضو هزينه اي از مرتبه Theta(n2) خواهد داشت Here, by the cost of n adds, we mean the cost of a sequence of n add operations performed on an initially empty linear list.

19 طول آرايه جديد چقدر بايد باشد؟
اگر طول جديد مضربي از طول فعلي باشد: new length = c * old length حافظه مورد نياز برابر است با: (1+c) * (old length) اگر طول جديد برابر طول فعلي + مقدار ثابتي باشد، حافظه مورد نياز برابر است با: (old length) + (old length + c) = 2 * (old length) + c space.

20 مديريت طول ليست در جاوا java.util.Vector … array doubling
java.util.ArrayList … c = 1.5 Java provides 2 classes that use an array to represent a linear list—Vector and ArrayList. ArrayLinearList of text does array doubling.

21 پياده سازي آرايه هاي يك بعدي در Java, C, C++
Memory a b c d start 1-dimensional array x = [a, b, c, d] map into contiguous memory locations location(x[i]) = start + i

22 حافظه مورد نياز براي نگهداري اطلاعات جنبي Overhead
Memory a b c d start What about #bytes/element of array? Excluded in above analysis. space overhead = 4 bytes for start + 4 bytes for x.length = 8 bytes

23 آرايه هاي دو بعدي تعريف آرايه دو بعدي int [][]a = new int[3][4];
may be shown as a table a[0][0] a[0][1] a[0][2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3] a[2][0] a[2][1] a[2][2] a[2][3]

24 سطرهاي آرايه دو بعدي a[0][0] a[0][1] a[0][2] a[0][3] row 0

25 ستون هاي آرايه دو بعدي column 0 column 1 column 2 column 3
a[0][0] a[0][1] a[0][2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3] a[2][0] a[2][1] a[2][2] a[2][3]

26 پياده سازي آرايه هاي دو بعدي در Java, C, C++
2-dimensional array x a, b, c, d e, f, g, h i, j, k, l آرايه دو بعدي، آرايه از آرايه هاي سطري است x = [row0, row1, row 2] row 0 = [a,b, c, d] row 1 = [e, f, g, h] row 2 = [i, j, k, l]

27 پياده سازي آرايه هاي دو بعدي در Java, C, C++
b c d e f g h i j k l x[] x.length = 3 x[0].length = x[1].length = x[2].length = 4 The 4 separate arrays are x, x[0], x[1], and x[2].

28 space overhead = overhead for 4 1D arrays = 4 * 8 bytes = 32 bytes
حافظه جنبي مورد نياز: a b c d e f g h i j k l x[] space overhead = overhead for 4 1D arrays = 4 * 8 bytes = 32 bytes = (number of rows + 1) x 8 bytes

29 پياده سازي آرايه هاي دو بعدي در Java, C, C++
b c d e f g h i j k l x[] آرايه اي از آرايه ها قطعاتي از حافظه به طول هاي 3و4و4و4 مورد نياز است يك قطه پيوسته براي نگهداري آرايه اصلي و سه قطعه پيوسته براي نگهداري سطرها مورد نياز است

30 نگاشت سطري Row-Major Mapping
مثال:آرايه 3 4 x: a b c d e f g h i j k l آرايه خطي از آرايه دوبعدي توليد مي شود سطرهاي آرايه دو بعدي از چپ به راست و از بالا به پايين در اين آرايه قرار مي گيرند آرايه يك بعدي:‌ y[] = {a, b, c, d, e, f, g, h, i, j, k, l} مشكل اين نگاشت: ‌بلوك پيوسته بزرگي از حافظه مورد نياز است Used in Pascal and Fortran for example. row 0 row 1 row 2 row i

31 يافتن x[i][j] فرض كنيد x‌ داراي r سطر و c‌ستون است هر سطر c عضو دارد
row 0 row 1 row 2 row i c 2c 3c ic فرض كنيد x‌ داراي r سطر و c‌ستون است هر سطر c عضو دارد قبل از سطر i به تعداد i‌سطر وجود دارند(شروع از صفر) بنابراين x[i][j]به ic + j‌ در آرايه 1 بعدي نگاشت مي شود

32 Space Overhead 4 bytes for start of 1D array +
row 0 row 1 row 2 row i 4 bytes for start of 1D array + 4 bytes for length of 1D array + 4 bytes for c (number of columns) = 12 bytes (number of rows = length /c) Although, for our example the difference in space overhead between the two representation methods is small, 20 bytes, the space overhead for the row-major scheme is 12 bytes regardless of the number of rows in the array. In the array of arrays scheme, the space overhead is 8 * (r+1) bytes. So the overhead for an array with rows Is bytes when the array of array scheme is used and only 12 bytes when the row-major scheme is used.

33 Column-Major Mapping a b c d e f g h i j k l
اعضاي آرايه دو بعدي ، ستون به ستون از چپ به راست و از بالا به پايين در آرايه يك بعدي قرار مي گيرند y = {a, e, i, b, f, j, c, g, k, d, h, l} مشكل نگاشت سطري را دارد

34 ماتريس Matrix ماتريس شامل جدولي از اعضاست كه سطر وستون دارد. انديس سطر وستون از 1 شروع مي شود a b c d row 1 e f g h row 2 i j k l row 3 از x(i,j) بجاي x[i][j]. استفاده مي شود مي توان از آرايه دو بعدي براي نمايش ماتريس استفاده كرد

35 معايب استفاده از آرايه دو بعدي براي نمايش ماتريس
شروع انديس آرايه از صفر آرايه هاي جاوا از اعمال ويژه ماتريس ها مانند جمع ، ضرب و ترانهاده كردن پشتيباني نمي كنند لازم است ساختار داده ويژه اي براي نمايش ماتريس تعريف شود The class Matrix uses the row-major representation of a matrix.

36 ماتريس قطري x(i,j) is on diagonal iff i = j
ماتريس مربعي است كه اعضاي غير قطر اصلي آن صفر باشند x(i,j) is on diagonal iff i = j number of diagonal elements in an n x n matrix is n non diagonal elements are zero store diagonal only vs n2 whole

37 ماتريس پايين مثلثي ماتريس پايين مثلثي: ماتريس مربعي كه غير از اعضاي قطر اصلي و زير آن بقيه اعضا صفر هستند: تعداد اعضاي غير صفر ماتريس پايين مثلثي : … + n = n(n+1)/2. پياده سازي: آرايه يك بعدي از اين اعضا

38 پياده سازي با آرايه اي از آرايه ها
1 2 3 4 5 6 x[] 7 8 9 l0 آرايه دوبعدي نامنظم: آرايه هايي با طول هاي مختلف براي نگهداري اعضا

39 توليد آرايه هاي نامنظم در جاوا
// declare a two-dimensional array variable // and allocate the desired number of rows int [][] irregularArray = new int [numberOfRows][]; // now allocate space for the elements in each row for (int i = 0; i < numberOfRows; i++) irregularArray[i] = new int [size[i]]; // use the array like any regular array irregularArray[2][3] = 5; irregularArray[4][6] = irregularArray[2][3] + 2; irregularArray[1][1] += 3;

40 استفاده از يك آرايه يك بعدي
از روش سطري براي چيدن اعضا استفاده مي شود ولي اعضاي صفر حذف مي شوند 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 If the 1D array that is used to store the lower triangle is y, then following the above mapping y[i] = i +1 . y[] = {1, 2, 3, 4, …, 10}

41 قبل از سطر i سطرهاي 1, 2, …, i-1 قرار دارند طول سطر i برابر i. است
يافتن عضو [i][j] r 1 r2 r3 row i 1 3 6 ترتيب: : row 1, row 2, row 3, … قبل از سطر i سطرهاي 1, 2, …, i-1 قرار دارند طول سطر i برابر i. است تعداد اعضايي كه قبل از سطر i قرار دارند: … + i-1 = i(i-1)/2 پس عضو (i,j) در مكان i(i-1)/2 + j -1 آرايه 1 بعدي قرار دارد. When using the irregular array mapping of a lower triangular matrix we are able to use conventional Java syntax to access elements, but for large n, the space overhead is quite a bit more than when the row-major mapping into a 1D array is used. The row-major mapping requires you to develop a formula to access elements.

42 ماتريس خلوت Sparse Matrices
ماتريس خلوت:‌اغلب اعضا صفرند ماتريس متراكم: اعضاي معدودي صفرند sparse … many elements are zero dense … few elements are zero مثال : ماتريس هاي قطري، ماتريس هاي خلوتي هستند مي توان از آرايه يك بعدي براي نمايش آنها استفاده كرد اين نوع ماتريس هاي خلوت، به ماتريس هاي ساختاريافته معروفند

43 ماتريس خلوت بدون ساختار معلوم
مثال: جدول پرواز ها. فرودگاهها از 1 تا n‌ شماره گذاري شده اند flight(i,j) = نشان دهنده تعداد پروازهاي مستقيم بين اين فرودگاههاست اگر مثلا: n = 1000 n x n array of list references => 4 million bytes اگر تعداد واقعي پروازها 20,000 باشد. حداكثر ركورد ذخيره مي شود Assume that flight(i,j) = null if there is no flight from i to j. Of the 1 million entries in the flight matrix at most 20,000 can be nonnull as each nonnull entry represents at least 1 different flight. So we need to store at most 20,000 nonnull entries.

44 ماتريس خلوت بدون ساختار معلوم
ماتريس صفحات وب:. صفحات از 1 تا n شماره گذاري شده اند تعداد لينكها از صفحه i‌به صفحه web(i,j) = j آناليز وب. authority page … page that has many links to it hub page … links to many authority pages Web page analyses are done using a matrix such as web(*,*). See IEEE Computer, August 1999, p 60, for a paper that describes Web analysis based on such a matrix. Operations such as matrix transpose and multiply are used.

45 ماتريس صفحات وب n = 2 billion (and growing by 1 million a day)
n x n array of ints => 16 * 1018 bytes (16 * 109 GB) به طور متوسط هر صفحه به 10 صفحه ديگر لينك دارد پس در هر سطر آرايه به طور متوسط 10 عضو غير صفر وجود دارد فضاي مورد نياز براي نگهداري اعضاي غير صفر برابر است با: 20 billion x 4 bytes = 80 billion bytes (80 GB) On average, 10 entries in each row are nonzero.

46 نمايش ماتريس خلوت بدون ساختار
آرايه يك بعدي با چينش سطري: هر عضو غير صفر با سه تايي (row, column, value) تعيين مي شود اين سه تايي را مي توان با آرايه هاي سه تايي يا ليست پيوندي يا يك ركورد نشان داد

47 نمايش با استفاده از ليست خطي
list = row column value The single linear list has 6 elements. Each list element is a triple (row, column, value).

48 نمايش با استفاده از ليست خطي
row list = column value element row column value The list and array linear list representation drawings are essentially the same, because of the identity mapping (element[i] = list element i) that is used.

49 نمايش با زنجيره اي از ركوردها
ساختار هر ركورد row col next value The drawing, above, assumes a custom class for matrices. This custom class does not use the class Chain. If you want to use the class Chain, the fields row, col, and value need to be in another class, say Data. The elements of Chain will have the data type Data.

50 row 1 1 2 2 4 4 list = column 3 5 3 4 2 3 value 3 4 5 7 2 6 firstNode
يك زنجيره row list = column value 1 3 5 4 2 7 6 null firstNode

51 هر سطر در يك ليست جداگانه
row1 = [(3, 3), (5,4)] row2 = [(3,5), (4,7)] row3 = [] row4 = [(2,2), (3,6)] Array linear list per row.

52 ساختار هر ركورد زنجيره اي از سطرها next value col
The above drawing assumes a custom class for sparse matrices. If you want to use the class Chain, the col and value fields will be members of some other class (say) Data. The data type of the elements in the chains will be Data.

53 آرايه اي از زنجيره سطرها
row[] 3 null 4 5 5 3 null 7 4 null When using the class Chain for each row chain, the array row is declared as Chain [] row = new Chain[numberOfRows]; After row[i] is initialized by calling the constructor for Chain, row[i].firstNode gets you to the first node in the chain for row i. Book has a chain of row chains. In this case only non-empty row chains are present. 2 null 6 3

54 نمايش با ليستهاي متعامد سطري و ستوني
ساختار ركورد row col next down value

55 ليست سطرها null 1 3 5 4 2 7 6 n

56 ليست ستون ها 1 3 5 4 2 7 6 n

57 0 0 3 0 4 0 0 5 7 0 0 0 0 0 0 0 2 6 0 0 Orthogonal Lists row[] 1 3 5 4
null row[] 1 3 5 4 2 7 6 n

58 حافظه مورد نياز 500 x 500 matrix with 1994 nonzero elements
2D array x 500 x 4 = 1million bytes Single Array List 3 x 1994 x 4 = 23,928 bytes One Chain Per Row x 4 = 25,928

59 زمان اجرا Matrix Transpose 500 x 500 matrix with 1994 nonzero elements
2D array ms Single Array List ms One Chain Per Row ms

60 زمان اجرا Matrix Addition.
500 x 500 matrices with 1994 and 999 nonzero elements 2D array ms Single Array List ms One Chain Per Row ms

61 تمرين – نمايش ماتريس خلوت
يك ADT براي نمايش ماتريس هاي خلوت با استفاده از ليست هاي خطي بنويسيد كه در آن هر عضو غير صفر با سه تايي (row, column, value) نشان داده مي شود. عمل ضرب دو ماتريس را براي اين نوع نمايش به صورت شبه كد بنويسيد تمرين را بفرستيد: [ds-cs-sparse] ‌ يا [ds-math-sparse]


Download ppt "بردارها و ماتريس ها ساختمان داده ها"

Similar presentations


Ads by Google