Data Structures: 1. Arrays

Download as pdf
Download as pdf
You are on page 1of 8

‫‪Data Structures‬‬

‫‪Notes for Lecture 2‬‬


‫‪Linear Data Structures‬‬
‫‪By‬‬
‫‪Samaher Hussein Ali‬‬
‫‪2007-2008‬‬

‫‪1. Arrays‬‬

‫ﺗﻌﺪ اﻟﻤﺼﻔﻮﻓﺎت ﻣﻦ اﻟﻬﻴﺎآﻞ اﻟﺒﻴﺎﻧﻴﺔ اﻟﺒﺴﻴﻄﺔ اﻟﺘﺮآﻴﺐ واﻻﺳﺘﺨﺪام وهﻲ ﺗﺴﺘﺨﺪم ﻣﻦ ﻗﺒﻞ اﻟﻤﺒﺮﻣﺞ ﺑﺒﺴﺎﻃﺔ دون اﻟﺤﺎﺟﺔ اﻟﻰ ﻣﻌﺮﻓﺔ ﺗﻘﻨﻴﺔ ﺧﺰﻧﻬﺎ‬
‫ﺑﺎﻟﺬاآﺮة وذﻟﻚ ﺑﺎﺳﺘﺨﺪام اﻟﻤﻌﺎدﻻت اﻟﺮﻳﺎﺿﻴﺔ‪.‬‬
‫اذن‪-:‬‬
‫اﻟﻤﺼﻔﻮﻓﺔ هﻲ ﻋﺒﺎرة ﻋﻦ ﻣﺠﻤﻤﻮﻋﺔ ﻣﻦ اﻟﻤﻮاﻗﻊ اﻟﺨﺰﻧﻴﺔ ﻓﻲ اﻟﺬاآﺮة او هﻲ ﻣﺠﻤﻮﻋﺔ ﻣﻦ اﻻزواج)اﻟﻘﻴﻢ و اﻟﻤﻮﻗﻊ( واﻟﻌﻤﻠﻴﺎت اﻟﺘﻲ ﺗﺤﺼﻞ ﻋﻠﻴﻬﺎ‬
‫هﻲ‪-:‬‬
‫‪1. Create‬‬ ‫‪empty array‬‬
‫‪2. Store‬‬ ‫‪a partial mapping from index to value‬‬
‫‪3. Retrieve‬‬ ‫‪use that mapping‬‬
‫ﻓﻮاﺋﺪهﺎ‪-:‬‬
‫‪ .1‬ﺳﻬﻠﺔ اﻻﺳﺘﺨﺪام‬
‫‪ .2‬ﺳﻬﻠﺔ اﻟﺘﻨﻔﻴﺬ‬
‫‪ .3‬اﻻﺣﺘﻔﺎظ ﺑﺎآﺜﺮ ﻣﻦ ﻗﻴﻤﺔ ﻟﻠﻤﺘﻐﻴﺮ‬
‫‪ .4‬ﺗﺴﻬﻴﻞ ﺑﻌﺾ اﻟﻤﻌﺎﻟﺠﺎت اﻟﻤﻌﻘﺪة‬
‫ﻣﺴﺎوﺋﻬﺎ‪-:‬‬
‫‪ .1‬اﻟﻮﺻﻮل ﻓﻴﻬﺎ ﻋﺸﻮاﺋﻲ‬
‫‪ .2‬ﻻﻳﻤﻜﻦ ﺣﺸﺮ او ﺣﺬف اي ﻣﻮﻗﻊ‬
‫‪ .3‬ﻻﻳﻤﻜﻦ ان ﻧﺨﺰن ﻓﻴﻬﺎ ﺳﻮى ﻧﻮﻋﻴﺔ واﺣﺪة‬
‫‪ .4‬اﻟﺨﺰن ﻓﻴﻬﺎ ﻏﻴﺮ دﻳﻨﺎﻣﻴﻜﻲ‬

‫‪One Dimensional Array:-‬‬


‫ﻓﻲ ﻟﻐﺔ ﻓﻴﺠﻮل ﺑﻴﺴﻚ ﻳﺘﻢ ﺗﻌﺮﻳﻒ اﻟﻤﺼﻔﻮﻓﺔ آﺎﻟﺘﺎﻟﻲ‪-:‬‬
‫‪Const n=10‬‬
‫}‪Redim X (1 to n) as integer :{ or any other type‬‬

‫واﻟﻤﻌﺎدﻟﺔ اﻟﻌﺎﻣﺔ اﻟﺘﻲ ﻳﺘﺤﺪد ﺑﻮاﺳﻄﺘﻬﺎ ﻋﻨﻮان اي ﻋﻨﺼﺮ ﻓﻲ اﻟﺬاآﺮة ﺑﻤﺼﻔﻮﻓﺔ اﺣﺎدﻳﺔ هﻲ‪-:‬‬

‫‪1‬‬
‫‪Loc (Name [I]) = Loc (Name [1]) + [I-1]* w‬‬

‫ﺣﻴﺚ‪-:‬‬
‫‪ Name‬هﻮ اﺳﻢ اﻟﻤﺼﻔﻮﻓﺔ‬
‫)]‪ Loc (Name [I‬هﻮ ﻣﻮﻗﻊ اﻟﻌﻨﺼﺮ ﻓﻲ اﻟﻤﺼﻔﻮﻓﺔ‬
‫)]‪ Loc (Name [1‬هﻮ ﻣﻮﻗﻊ اول ﻋﻨﺼﺮ ﻣﺼﻔﻮﻓﺔ ﻓﻲ اﻟﺬاآﺮة‬
‫‪ W‬هﻲ ﻋﺪد ارﻗﺎم اﻟﻜﻠﻤﺎت اﻟﻤﺤﺪدة ﻟﻜﻞ ﻋﻨﺼﺮ ﻓﻲ اﻟﻤﺼﻔﻮﻓﺔ وﻓﻲ ﺑﻌﺾ اﻻﺣﻴﺎن ﻻﺗﺬآﺮ ﻓﺘﻌﺘﺒﺮ )واﺣﺪ( او ﺗﻬﻤﻞ‬

‫]‪Loc (Name [I]) = Loc (Name [1]) + [I-1‬‬

‫]‪ A [3‬ﻓﻲ ﻣﺼﻔﻮﻓﺔ ﺗﺤﺘﻮي ﻋﻠﻰ ﺳﺒﻌﺔ ﻣﻮاﻗﻊ ﻋﻠﻤًﺎ ان اﻟﻌﻨﺼﺮ اﻷول ﻟﻠﻤﺼﻔﻮﻓﺔ‬ ‫ﻣﺜﺎل‪-:‬اوﺟﺪ ﻣﻮﻗﻊ اﻟﻌﻨﺼﺮ‬
‫ﻣﺨﺰون ﻓﻲ اﻟﻤﻮﻗﻊ‪200‬‬

‫‪a1‬‬ ‫‪a2‬‬ ‫‪a3‬‬ ‫‪a4‬‬ ‫‪a5‬‬ ‫‪a6‬‬ ‫‪a7‬‬

‫‪200‬‬ ‫‪201‬‬ ‫‪202‬‬ ‫‪203‬‬ ‫‪204‬‬ ‫‪205‬‬ ‫‪207‬‬

‫]‪Loc (A [3]) = Loc (A [1]) + [I-1‬‬


‫‪=200+3-1‬‬
‫‪=202‬‬
‫‪Two Dimensional Arrays:-‬‬
‫ﻓﻲ ﻟﻐﺔ ﻓﻴﺠﻮل ﺑﻴﺴﻚ ﻳﺘﻢ ﺗﻌﺮﻳﻔﻬﺎ آﺎﻟﺘﺎﻟﻲ‪-:‬‬
‫‪Const n=4‬‬
‫‪Const m=3‬‬
‫}‪Redim X(1 to n, 1 to m) as integer;{or any other type‬‬
‫اﻟﻬﻴﻜﻞ اﻟﻤﻨﻄﻘﻲ ﻟﻬﺎ‬
‫‪a11‬‬ ‫‪a12‬‬ ‫‪a13‬‬
‫‪a21‬‬ ‫‪a22‬‬ ‫‪a23‬‬
‫‪a31‬‬ ‫‪a32‬‬ ‫‪a33‬‬
‫‪a41‬‬ ‫‪a42‬‬ ‫‪a43‬‬

‫‪2‬‬
‫اﻟﻬﻴﻜﻞ اﻟﺘﺨﺰﻳﻨﻲ )اﻟﺬاآﺮة(‪-:‬‬
‫ان ﻋﻤﻠﻴﺔ ﺗﺤﻮﻳﻞ اﻟﺒﻴﺎﻧﺎت ﻣﻦ اﻟﻬﻴﻜﻞ اﻟﻤﻨﻄﻘﻲ اﻟﻰ اﻟﻬﻴﻜﻞ اﻟﺘﺨﺰﻳﻨﻲ ﻓﻲ اﻟﺬاآﺮة ﺗﺘﻢ ﻳﻄﺮﻳﻘﺘﻴﻦ‪-:‬‬
‫‪1. Row Wise‬‬
‫‪2. Column Wise‬‬

‫اﻣﺎ ﻓﻲ ﻟﻐﺔ ﺑﺎﺳﻜﺎل ﻓﻴﺘﻢ اﻟﺨﺰن ﻓﻴﻬﺎﺑﺎﻟﺬاآﺮة ﺑﺎﺳﺘﺨﺪام ﻃﺮﻳﻘﺔ اﻟﺼﻔﻮف اﻣﺎ ﻟﻐﺔ ﺑﻴﺴﻚ وآﻮﺑﻞ وﻓﻮرﺗﺮان ﻓﻴﺘﻢ اﻟﺨﺰن ﻓﻴﻬﺎ ﺑﺎﺳﺘﺨﺪام ﻃﺮﻳﻘﺔ‬
‫ﺧﺰن اﻻﻋﻤﺪة وذﻟﻚ ﺣﺴﺐ ﺗﺼﻤﻴﻢ ﻣﺘﺮﺟﻢ او ﻣﻔﺴﺮ ﺗﻠﻚ اﻟﻠﻐﺔ‪.‬‬

‫اﻟﻬﻴﻜﻞ اﻟﺘﺨﺰﻳﻨﻲ) اﻟﺬاآﺮة(‬

‫‪a11‬‬ ‫‪a12‬‬ ‫‪a13‬‬ ‫‪a21‬‬ ‫‪a22‬‬ ‫‪a23‬‬ ‫‪a31‬‬ ‫‪a32‬‬ ‫‪a33‬‬ ‫‪a41‬‬ ‫‪a42‬‬ ‫‪a43‬‬

‫‪Row Wise‬‬

‫‪a11‬‬ ‫‪a21‬‬ ‫‪a31‬‬ ‫‪a41‬‬ ‫‪a12‬‬ ‫‪a22‬‬ ‫‪a32‬‬ ‫‪a42‬‬ ‫‪a13‬‬ ‫‪a23‬‬ ‫‪a33‬‬ ‫‪a43‬‬

‫‪Colume Wise‬‬

‫‪Row Wise:-‬‬

‫ﺣﻴﺚ اﻧﻬﺎ ﻃﺮﻳﻘﺔ ﺗﺨﺰﻳﻨﻴﺔ ﻳﺘﻢ ﻓﻴﻬﺎ ﺗﺨﺰﻳﻦ ﺑﻴﺎﻧﺎت اﻟﺼﻒ اﻻول ﻓﻲ اﻟﺬاآﺮة وﻳﻌﺪهﺎ ﺑﻴﺎﻧﺎت اﻟﺼﻒ اﻟﺜﺎﻧﻲ واﻟﺜﺎﻟﺚ وهﻜﺬا‪.‬‬
‫اﻣﺎ اﻟﻤﻌﺎدﻟﺔ اﻟﺘﻲ ﻧﺤﺘﺎﺟﻬﺎ ﻟﺘﺮﺗﻴﺐ ﻋﻨﺎﺻﺮ اﻟﻤﺼﻔﻮﻓﺔ ﻓﻲ اﻟﺬاآﺮة ﻓﻬﻲ ‪-:‬‬

‫]‪Loc (A [I, J]) = Loc (A [1, 1]) + [I-1]*M+ [J-1‬‬


‫ﺣﻴﺚ‪-:‬‬
‫)]‪ -:Loc (A [1, 1‬ﻣﻮﻗﻊ ﺧﺰن اول ﻋﻨﺼﺮ ﻣﻦ اﻟﻤﺼﻔﻮﻓﺔ ﻓﻲ اﻟﺬاآﺮة‬
‫]‪ -: [I-1‬ﻋﺪد اﻟﺼﻔﻮف اﻟﺘﻲ ﺗﻘﻊ ﻗﺒﻞ ذﻟﻚ اﻟﻌﻨﺼﺮ‬
‫‪ -:M‬ﻋﺪد اﻻﻋﻤﺪة اﻟﻜﻠﻲ‬
‫]‪ -:[J-1‬ﻋﺪد اﻻﻋﻤﺪة اﻟﺘﻲ ﺗﻘﻊ ﻗﺒﻞ ذﻟﻚ اﻟﻌﻨﺼﺮ‬
‫‪Loc (A [I, J]) = Loc (A [1, 1]) +( [I-1]*M+ [J-1]) *W‬‬

‫‪3‬‬
‫ﻳﺘﻢ اﺳﺘﺨﺪام هﺬا اﻟﻘﺎﻧﻮن ﻓﻲ ﺣﺎﻟﺔ ﺗﺨﺼﻴﺺ ﻋﺪد ﻣﻦ اﻟﻜﻠﻤﺎت ﻟﻜﻞ ﻋﻨﺼﺮ‬

‫]‪ A [3, 4‬واﻟﻤﻮﻗﻊ اﻻﺑﺘﺪاﺋﻲ ﻟﺨﺰن ﻋﻨﺎﺻﺮهﺎ ﻓﻲ اﻟﺬاآﺮة هﻮ ‪ 1500‬ا ﺣﺴﺐ ﻣﻮﻗﻊ‬ ‫ﻣﺜﺎل اذا آﺎن ﻟﺪﻳﻚ اﻟﻤﺼﻔﻮﻓﺔ‬
‫ﺑﺎﺳﺘﺨﺪام ﻃﺮﻳﻘﺔ ﺧﺰن اﻟﺼﻔﻮف‬ ‫]‪A [2, 3‬‬ ‫اﻟﻌﻨﺼﺮ‬

‫]‪Loc (A [2, 3]) = Loc (A [1, 1]) + [I-1]*M+ [J-1‬‬


‫‪=1500+2-1*4+3-1‬‬
‫‪=1500+1*4+2‬‬
‫‪=1500+4+2‬‬
‫‪=1506‬‬

‫‪Column Wise:-‬‬
‫‪Loc (A [I, J]) = Loc (A [1, 1]) + ([J-1]*N+ [I-1])*W‬‬

‫ﺑﺎﺳﺘﺨﺪام ﻃﺮﻳﻘﺔ ﺧﺰن اﻻﻋﻤﺪة‪.‬‬ ‫]‪A [3, 2‬‬ ‫]‪ A [9, 3‬ﺟﺪ ﻣﻮﻗﻊ اﻟﻌﻨﺼﺮ‬ ‫ﻣﺜﺎل اذا آﺎن ﻟﺪﻳﻚ اﻟﻤﺼﻔﻮﻓﺔ‬
‫‪Loc (A [3, 2]) = Loc (A [1, 1]) + ([J-1]*N+ [I-1])*W‬‬
‫)‪=1+ (2-1)*9+ (3-1‬‬
‫‪=1+9+2‬‬
‫‪=12‬‬
‫]‪ B [5, 6‬واﻟﻤﻮﻗﻊ اﻻﺑﺘﺪاﺋﻲ ﻟﺨﺰن ﻋﻨﺎﺻﺮهﺎ ﻓﻲ اﻟﺬاآﺮة هﻮ ‪ 500‬وﻋﻠﻰ ﻓﺮض ﺣﺠﺰ‬ ‫ﻣﺜﺎل اذا آﺎن ﻟﺪﻳﻚ اﻟﻤﺼﻔﻮﻓﺔ‬
‫ﺑﺎﺳﺘﺨﺪام اﻟﻄﺮﻳﻘﺘﻴﻦ‪.‬‬ ‫]‪B [2, 3‬‬ ‫آﻠﻤﺘﻴﻦ ﻟﻜﻞ ﻋﻨﺼﺮ ﻣﻦ ﻋﻨﺎﺻﺮ اﻟﻤﺼﻔﻮﻓﺔ اﺣﺴﺐ ﻣﻮﻗﻊ اﻟﻌﻨﺼﺮ‬
‫‪Sol:-‬‬
‫‪N=5, M=6, I=2, J=3, W=2.‬‬
‫‪1. Loc (B [2, 3]) = Loc (B [1, 1]) + ([J-1]*N+ [I-1])*W‬‬
‫‪=500+ ((3-1)*5+ (2-1))*2‬‬
‫‪=500+11*2‬‬
‫‪=522‬‬
‫‪2. Loc (B [2, 3]) = Loc (B [1, 1]) + ([I-1]*M+ [J-1])*W‬‬
‫‪=500+ ((2-1)*6+ (3-1))*2‬‬
‫‪=500+8*2‬‬
‫‪=516‬‬

‫‪4‬‬
‫ﻣﻼﺣﻈﺎت‪-:‬‬
‫* ﻓﻲ ﺣﺎﻟﺔ ﻋﺪم ذآﺮ ﻗﻴﻤﺔ ﻣﻮﻗﻊ اﻟﻌﻨﺼﺮ اﻻﺑﺘﺪاﺋﻲ ﻓﺎﻧﻨﺎ ﻧﻌﺘﺒﺮﻩ واﺣﺪ‬
‫* ﻣﺘﺮﺟﻢ او ﻣﻔﺴﺮ اﻟﻠﻐﺔ ﻋﻨﺪﻣﺎ ﻳﺠﺪ ﺗﺼﺮﻳﺤًﺎ ﻟﻤﺼﻔﻮﻓﺔ ﺛﻨﺎﺋﻴﺔ ﻓﺎﻧﻪ ﻳﻘﻮم ﺑﻤﺎ ﻳﺎﺗﻲ‪-:‬‬

‫وﺗﺒﺪا ﺑﻌﻨﻮان ﻣﻄﻠﻖ ﻓﻲ اﻟﺬاآﺮة ﻳﻘﺎﺑﻞ اﻻﺳﻢ اﻟﻤﻨﻄﻘﻲ اﻟﻤﺼﺮح ﻓﻲ اﻟﺒﺮﻧﺎﻣﺞ‬ ‫‪N*M‬‬ ‫‪ .1‬ﺗﺤﻀﻴﺮ ﻣﻨﻄﻘﺔ ﺧﺰﻧﻴﺔ ﺑﺴﻌﺔ‬
‫‪ .2‬ﻳﺴﺘﺨﺪم ﻣﺘﺮﺟﻢ اﻟﻠﻐﺔ اﻟﻤﻌﺎدﻻت اﻟﺮﻳﺎﺿﻴﺔ اﻟﺘﻲ وﺿﺤﻨﺎهﺎ اﻋﻼﻩ ﻟﺘﺤﺪﻳﺪ ﻋﻨﻮان اي ﻗﻴﻤﺔ ﻓﻲ اﻟﻤﺼﻔﻮﻓﺔ‪.‬‬

‫‪Three Dimensional Arrays:-‬‬

‫]‪A [L1...U1, L2...U2, L3...U3‬‬


‫ﻋﻨﺪﻣﺎ ﺗﻜﻮن اﻟﻤﺼﻔﻮﻓﺔ ﺛﻼﺛﻴﺔ اﻻﺑﻌﺎد‬
‫ﺣﻴﺚ‪-:‬‬
‫‪ L1...U1‬ﺗﻤﺜﻞ ﻋﺪد اﻟﻤﺴﺘﻮﻳﺎت ﻟﻠﻤﺼﻔﻮﻓﺔ‬
‫‪ L2...U2‬ﺗﻤﺜﻞ ﻋﺪد اﻟﺼﻔﻮف‬
‫‪ L3...U3‬ﺗﻤﺜﻞ ﻋﺪد اﻻﻋﻤﺪة‬
‫‪U2*U3‬‬ ‫ﺣﻴﺚ ان هﺬﻩ اﻟﻤﺼﻔﻮﻓﺔ ﺗﻔﻬﻢ ﻋﻠﻰ اﺳﺎس اﻧﻪ ‪ U1‬ﻳﻤﺜﻞ ﻋﺪد اﻟﻤﺼﻔﻮﻓﺎت اﻟﺜﻨﺎﺋﻴﺔ اﻻﺑﻌﺎد وهﻲ‬
‫وﻳﻤﻜﻦ اﻳﺠﺎد ﻣﻮﻗﻊ اي ﻋﻨﺼﺮ ﻓﻴﻬﺎ ﺑﻄﺮﻳﻘﺔ ﺗﺨﺰﻳﻦ اﻟﺼﻔﻮف ﺣﺴﺐ اﻟﻤﻌﺎدﻟﺔ اﻻﺗﻴﺔ‪-:‬‬

‫‪Loc (A [I, J, K]) = Loc (A [1, 1, 1]) + ([I-1]*U2*U3+ [J-1]*U3+ (K-1))*W‬‬

‫اﻣﺎ ﺑﻄﺮﻳﻘﺔ ﺗﺨﺰﻳﻦ اﻻﻋﻤﺪة ﻓﺤﺴﺐ اﻟﻤﻌﺎدﻟﺔ اﻻﺗﻴﺔ‪-:‬‬


‫‪Loc (A [I, J, K]) = Loc (A [1, 1, 1]) + ([I-1]*U2*U3+ [K-1]*U2+ (J-1))*W‬‬

‫ﻓﻲ ﻣﺼﻔﻮﻓﺔ أﺑﻌﺎدهﺎ ‪ 2*3*5‬ﺑﻄﺮﻳﻘﺘﻲ ﺧﺰن اﻟﺼﻔﻮف واﻷﻋﻤﺪة‬ ‫]‪A [2, 2, 3‬‬ ‫ﻣﺜﺎل‪-:‬اوﺟﺪ ﻣﻮﻗﻊ اﻟﻌﻨﺼﺮ‬
‫‪Sol:-‬‬
‫‪I=2, J=2, K=3, U2=3, U3=5‬‬
‫‪U1= (I-1)*U2*U3= (2-1)*3*5 =15‬‬
‫)‪1.Row Wise:- Loc (A [2, 2, 3])=Loc(A[1,1,1])+ (I-1)*U2*U3+(J-1)*U3+(K-1‬‬
‫)‪=1+15+ (2-1)*5+ (3-1‬‬
‫‪=1+15+5+2‬‬
‫‪=23‬‬
‫)‪2. Column Wise: - Loc (A [2, 2, 3]) = Loc (A [1, 1, 1]) + [I-1]*U2*U3+ [K-1]*U2+ (J-1‬‬
‫)‪=1+15+ (3-1)*3+ (2-1‬‬
‫‪= 1+15+6+1=23‬‬
‫‪5‬‬
‫وﺑﺸﻜﻞ ﻋﺎم ﻳﻤﻜﻦ آﺘﺎﺑﺔ آﻼ اﻟﻘﺎﻧﻮﻧﻴﻦ اﻋﻼﻩ ﺑﺎﻟﺼﻴﻐﺔ اﻟﻌﺎﻣﺔ اﻟﺘﺎﻟﻴﺔ‪-:‬‬
‫‪1. Row Wise:-‬‬
‫)‪Loc (A [I, J, K]) = α+ [I-1]*(U2-L2+1)*(U3-L3+1) + [J-1]*(U3-L3+1) + (K-1‬‬
‫‪2. Column Wise:-‬‬
‫)‪Loc (A [I, J, K]) = α+ [I-1]*(U2-L2+1)*(U3-L3+1) + [K-1]*(U2-L2+1) + (J-1‬‬

‫‪Triangular Matrix‬‬
‫اﻟﻤﺼﻔﻮﻓﺎت اﻟﻤﺜﻠﺜﻴﺔ وهﻲ ﻧﻮﻋﺎن‪-:‬‬
‫‪1. Lower Triangular Matrix‬‬
‫‪2. Upper Triangular Matrix‬‬

‫ﻣﺼﻔﻮﻓﺔ ﻣﺜﻠﺜﻴﺔ ﺳﻔﻠﻰ‬ ‫ﻣﺼﻔﻮﻓﺔ ﻣﺜﻠﺜﻴﺔ ﻋﻠﻴﺎ‬

‫‪a11‬‬ ‫‪a12‬‬ ‫‪a13‬‬ ‫‪a14‬‬


‫‪a11‬‬ ‫‪0‬‬ ‫‪0‬‬ ‫‪0‬‬ ‫‪0‬‬ ‫‪a22‬‬ ‫‪a23‬‬ ‫‪a24‬‬
‫=‪A‬‬ ‫‪a21‬‬ ‫‪a22‬‬ ‫‪0‬‬ ‫‪0‬‬ ‫‪0‬‬ ‫‪0‬‬ ‫‪a33‬‬ ‫‪a34‬‬
‫‪a31‬‬ ‫‪a32‬‬ ‫‪a33‬‬ ‫‪0‬‬ ‫‪0‬‬ ‫‪0‬‬ ‫‪0‬‬ ‫‪a44‬‬
‫‪a41‬‬ ‫‪a42‬‬ ‫‪a43‬‬ ‫‪a44‬‬

‫‪IF I<J Then‬‬ ‫‪AIJ =0‬‬ ‫‪IF‬‬ ‫‪I>J‬‬ ‫‪Then‬‬ ‫‪AIJ=0‬‬

‫ﻣﻼﺣﻈﺔ‪-:‬‬
‫ان ﺗﺨﺰﻳﻦ اﻟﻤﺼﻔﻮﻓﺔ ﺑﺎﻟﺸﻜﻞ اﻟﺜﻨﺎﺋﻲ اﻋﻼﻩ ﻳﻌﻨﻲ ﺣﺠﺰ ﻣﺎ ﻳﻘﺮب ﻣﻦ ﺿﻌﻒ اﻟﻤﺴﺎﺣﺔ اﻟﺘﺨﺰﻳﻨﻴﺔ ﻟﻠﻤﺼﻔﻮﻗﺘﻴﻦ اﻋﻼﻩ ﺑﺎﻟﻘﻴﻤﺔ ﺻﻔﺮ وﺗﻜﻮن‬
‫اﻟﺨﺴﺎرة آﺒﻴﺮة ﻟﻮ آﺎﻧﺖ اﻟﻤﺼﻔﻮﻓﺔ اآﺒﺮ ﻣﻦ هﺬا ﻳﺘﻀﺢ اهﻤﻴﺔ هﻴﻜﻠﺔ اﻟﻤﺼﻔﻮﻓﺔ اﻟﻤﺮﺑﻌﺔ واﻋﺘﺒﺎرهﺎ ﻣﺼﻔﻮﻓﺔ ﻣﺜﻠﺜﻴﺔ ﻓﻘﻂ‪.‬‬
‫‪Lower Triangular Matrix‬‬
‫هﻲ اﻟﻤﺼﻔﻮﻓﺔ اﻟﺘﻲ ﺗﻜﻮن ﻓﻴﻬﺎ ﺟﻤﻴﻊ ﻋﻨﺎﺻﺮ اﻟﻤﺤﻮر اﻟﺮﺋﻴﺴﻲ واﻟﻌﻨﺎﺻﺮ اﻟﻮاﻗﻌﺔ أﺳﻔﻞ اﻟﻤﺤﻮر اﻟﺮﺋﻴﺴﻲ ﻗﻴﻢ ﺣﻘﻴﻘﻴﺔ ﻣﺎﻋﺪا ذﻟﻚ ﺗﻜﻮن‬
‫أﺻﻔﺎر أي إذا آﺎﻧﺖ ﻗﻴﻤﺔ ﻋﺪاد اﻟﺼﻔﻮف اﺻﻐﺮ ﻣﻦ ﻗﻴﻤﺔ ﻋﺪاد اﻷﻋﻤﺪة ﻓﺎن ﻗﻴﻤﺔ اﻟﻤﻮﻗﻊ ﻓﻲ اﻟﻤﺼﻔﻮﻓﺔ ﻳﺴﺎوي ﺻﻔﺮ‪.‬‬

‫‪a11‬‬ ‫‪0‬‬ ‫‪0‬‬ ‫‪0‬‬ ‫هﻴﻜﻞ ﻣﻨﻄﻘﻲ‬


‫‪a21‬‬ ‫‪a22‬‬ ‫‪0‬‬ ‫‪0‬‬
‫‪a31‬‬ ‫‪a32‬‬ ‫‪a33‬‬ ‫‪0‬‬
‫‪a41‬‬ ‫‪a42‬‬ ‫‪a43‬‬ ‫‪a44‬‬

‫ﻣﺼﻔﻮﻓﺔ ﻣﺜﻠﺜﻴﺔ ﺳﻔﻠﻰ‬


‫هﻴﻜﻞ ﺗﺨﺰﻳﻨﻲ‬

‫‪a11 a21 a22 a31 a32 a33 a41 a42 a43‬‬ ‫‪a44‬‬
‫‪Row Wise‬‬

‫‪a11 a21 a31 a41 a22 a32 a42 a33 a43‬‬ ‫‪a44‬‬ ‫‪Column Wise‬‬

‫‪6‬‬
‫‪Row Wise‬‬ ‫اﻟﻤﻌﺎدﻟﺔ اﻟﺘﺎﻟﻴﺔ ﺗﺤﺪد ﻣﻮﻗﻊ اي ﻋﻨﺼﺮ ﻓﻲ ﻣﺼﻔﻮﻓﺔ ﻣﺜﻠﺜﻴﺔ ﺳﻔﻠﻰ ﻋﻨﺪﻣﺎ ﺗﺨﺰن ﻓﻲ اﻟﺬاآﺮة ﺑﻄﺮﻳﻘﺔ اﻟﺼﻔﻮف‬

‫]‪Loc (A [I, J]) = Loc (A [1, 1]) + [I-1]*I/2+ [J-1‬‬

‫‪Column Wise‬‬ ‫اﻣﺎ ﺑﻄﺮﻳﻘﺔ اﻻﻋﻤﺪة‬


‫]‪Loc (A [I, J]) = Loc (A [1, 1]) + ([j-1]*N- [J-1]*J/2) + [I-1‬‬

‫ﻓﻲ ﻣﺼﻔﻮﻓﺔ ﻣﺜﻠﺜﻴﺔ ﺳﻔﻠﻰ أﺑﻌﺎدهﺎ‪ 4*4‬ﺑﻄﺮﻳﻘﺘﻲ ﺧﺰن اﻟﺼﻔﻮف واﻷﻋﻤﺪة‬ ‫]‪A [3, 2‬‬ ‫ﻣﺜﺎل‪-:‬اوﺟﺪ ﻣﻮﻗﻊ اﻟﻌﻨﺼﺮ‬
‫•‬ ‫]‪Row wise: Loc (A [3, 2]) = Loc (A [1, 1]) + [3-1]*3/2+ [2-1‬‬
‫‪=1+2*1.5+1‬‬
‫‪=1+3+1‬‬
‫‪=5‬‬
‫•‬ ‫]‪Column Wise: Loc (A [3, 2]) = Loc (A [1, 1]) + ([2-1]*4- [2-1]*2/2) + [3-1‬‬
‫‪=1+4-1+2‬‬
‫‪=6‬‬
‫‪Upper Triangular Matrix‬‬
‫هﻲ اﻟﻤﺼﻔﻮﻓﺔ اﻟﺘ ﻲ ﺗﻜ ﻮن ﻓﻴﻬ ﺎ ﺟﻤﻴ ﻊ ﻋﻨﺎﺻ ﺮ اﻟﻤﺤ ﻮر اﻟﺮﺋﻴ ﺴﻲ واﻟﻌﻨﺎﺻ ﺮ اﻟﻮاﻗﻌ ﺔ أﻋﻠ ﻰ اﻟﻤﺤ ﻮر اﻟﺮﺋﻴ ﺴﻲ ﻗ ﻴﻢ ﺣﻘﻴﻘﻴ ﺔ ﻣﺎﻋ ﺪا ذﻟ ﻚ ﺗﻜ ﻮن‬
‫أﺻﻔﺎر أي إذا آﺎﻧﺖ ﻗﻴﻤﺔ ﻋﺪاد اﻟﺼﻔﻮف اآﺒﺮ ﻣﻦ ﻋﺪاد اﻷﻋﻤﺪة ﻓﺎن ﻗﻴﻤﺔ اﻟﻤﻮﻗﻊ ﻓﻲ اﻟﻤﺼﻔﻮﻓﺔ ﻳﺴﺎوي ﺻﻔﺮ‪.‬‬
‫هﻴﻜﻞ ﻣﻨﻄﻘﻲ‬
‫‪a11‬‬ ‫‪a12‬‬ ‫‪a13‬‬ ‫‪a14‬‬
‫‪0‬‬ ‫‪a22‬‬ ‫‪a23‬‬ ‫‪a24‬‬
‫‪0‬‬ ‫‪0‬‬ ‫‪a33‬‬ ‫‪a34‬‬
‫‪0‬‬ ‫‪0‬‬ ‫‪0‬‬ ‫‪a44‬‬

‫ﻣﺼﻔﻮﻓﺔ ﻣﺜﻠﺜﻴﺔ ﻋﻠﻴﺎ‬


‫هﻴﻜﻞ ﺗﺨﺰﻳﻨﻲ‬

‫‪a11 a12 a13 a14 a22 a23 a24 a33 a34‬‬ ‫‪a44‬‬
‫‪Row Wise‬‬

‫‪a11 a12 a22 a13 a23 a33 a14 a24 a34‬‬ ‫‪a44‬‬ ‫‪Column Wise‬‬

‫اﻣﺎ ﺑﺎﻟﻨﺴﺒﺔ ﻟﻠﻤﺼﻔﻮﻓﺔ اﻟﻤﺜﻠﺜﻴﺔ اﻟﻌﻠﻴﺎ ﻓﺎن اﻟﻤﻌﺎدﻟﺔ اﻟﺘﻲ ﺗﺤﺪد ﻣﻮﻗﻊ اي ﻋﻨﺼﺮﻣﻦ ﻋﻨﺎﺻﺮهﺎ ﻓﻲ اﻟﺬاآﺮة ﺑﻄﺮﻳﻘﺔ ﺗﺨﺰﻳﻦ اﻻﻋﻤﺪة‬

‫‪Column Wise‬‬

‫]‪Loc (A [I, J]) = Loc (A [1, 1]) + [J-1]*J/2+ [I-1‬‬

‫‪7‬‬
‫‪Row Wise‬‬

‫]‪Loc (A [I, J]) = Loc (A [1, 1]) + ([I-1]*M- [I-1]*I/2) + [J-1‬‬

‫ﻓﻲ ﻣﺼﻔﻮﻓﺔ ﻣﺜﻠﺜﻴﺔ ﻋﻠﻴﺎ أﺑﻌﺎدهﺎ‪ 4*4‬ﺑﻄﺮﻳﻘﺘﻲ ﺧﺰن اﻟﺼﻔﻮف واﻷﻋﻤﺪة‬ ‫]‪A [2, 3‬‬ ‫ﻣﺜﺎل‪-:‬اوﺟﺪ ﻣﻮﻗﻊ اﻟﻌﻨﺼﺮ‬
‫•‬ ‫]‪Row wise: Loc (A [2, 3])= Loc (A [1, 1]) + ([2-1]*4- [2-1]*2/2) + [3-1‬‬
‫‪=1+(4-1)+2‬‬
‫‪=1+3+2‬‬
‫‪=6‬‬
‫•‬ ‫]‪Column Wise: Loc (A [2, 3]) = Loc (A [1, 1]) + [3-1]*3/2) + [2-1‬‬
‫‪=1+2*1.5+1‬‬
‫‪=1+3+1‬‬
‫‪=5‬‬

‫‪Representation of Arrays‬‬

‫ﻳﺠﺐ ﺗﻨﻔﻴﺬ ﻣﺼﻔﻮﻓﺔ ذات ‪ N‬ﻣﻦ اﻹﺑﻌﺎد ﻓﻲ ذاآﺮة ذات ﺑﻌﺪ واﺣﺪ اذن ﻋﺪد اﻟﻜﻠﻤﺎت اﻟﻤﻄﻠﻮﺑﺔ هﻲ ﺑﻌﺪد اﻟﻌﻨﺎﺻﺮ ﻓﻲ اﻟﻤﺼﻔﻮﻓﺔ ‪ .‬ﻓﺎذا آﺎﻧﺖ‬
‫اﻟﻤﺼﻔﻮﻓﺔ ﻣﻌﺮﻓﺔ آﺎﻟﺘﺎﻟﻲ ‪-:‬‬
‫] ‪A [L1...U1, L2...U2, L3...U3, …, Ln…Un‬‬
‫ﻓﺎن ﻋﺪد ﻋﻨﺎﺻﺮهﺎ ﻳﻤﻜﻦ ﻣﻌﺮﻓﺘﻪ ﺣﺴﺐ اﻟﻤﻌﺎدﻟﺔ اﻟﺘﺎﻟﻴﺔ‪-:‬‬

‫‪Μ‬‬ ‫)‪− L i ) + 1‬‬


‫‪n‬‬
‫‪i =1‬‬
‫‪(( U‬‬ ‫‪i‬‬

‫ﺣﻴﺚ‪-:‬‬
‫‪ n‬هﻲ ﻋﺪد اﺑﻌﺎد اﻟﻤﺼﻔﻮﻓﺔ‬

‫]‪A [2...5, 6...8‬‬ ‫ﻣﺜﺎل‪ -:‬اوﺟﺪ ﻋﺪد اﻟﻤﻮاﻗﻊ اﻟﻤﻄﻠﻮﺑﺔ ﻟﺨﺰن اﻟﻤﺼﻔﻮﻓﺔ‬

‫‪Μ‬‬
‫‪2‬‬
‫‪i =1‬‬
‫‪(( U 1 − L 1 ) + 1 ) * (( U‬‬ ‫‪2‬‬ ‫)‪− L 2 ) + 1‬‬
‫‪= (5-2+1)*(8-6+1) = 4*3 =12‬‬

‫‪8‬‬

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy