Lecture 7

  • April 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Lecture 7 as PDF for free.

More details

  • Words: 723
  • Pages: 5
‫‪Data Structures‬‬ ‫‪Notes for Lecture 7‬‬ ‫‪Non Linear Data Structures‬‬ ‫‪By‬‬ ‫‪Samaher Hussein Ali‬‬ ‫‪2007-2008‬‬

‫‪Graphics‬‬ ‫اﻟﻤﺨﻄﻂ هﻮ ﻋﺒﺎرة ﻋﻦ ﻣﺠﻤﻮﻋﺔ ﻣﻦ اﻟﻌﻨﺎﺻﺮ ﺗﻤﺜﻞ ﺑﻨﻘﺎط )رؤوس(ﺗﺴﻤﻰ ) ‪ ( vertices‬وهﺬﻩ اﻟﻌﻨﺎﺻﺮ ﺗﺮﺑﻄﻬﺎ ﻋﻼﻗﺎت ﺗﻤﺜﻞ ﺑﺨﻄﻮط‬ ‫ﺗﺴﻤﻰ اﻟﺤﻮاف)‬

‫‪. ( edges‬‬

‫ﻣﺜﺎل‪-:‬‬

‫‪1‬‬

‫‪3‬‬

‫‪6‬‬

‫‪2‬‬

‫‪4‬‬

‫‪5‬‬

‫)‪G=(V,E‬‬ ‫}‪V(G)={1,2,3,4,5,6‬‬ ‫})‪E(G)={(1,2).(1,3),(2,3),(2,4),(3,5),(3,6‬‬ ‫اﻟﻤﺨﻄﻄﺎت ﻧﻮﻋﻴﻦ‪-:‬‬ ‫* ﻣﺨﻄﻂ ﻏﻴﺮ ﻣﺘﺠﻪ ‪Undirected Graph‬‬ ‫هﻮ اﻟﻤﺨﻄﻂ اﻟﺬي ﺗﻜﻮن اﻟﻌﻼﻗﺔ ﺑﻴﻦ ﻋﻨﺎﺻﺮﻩ ﻏﻴﺮ ﻣﺮﺗﺒﺔ أي ان اﻻﺗﺠﺎﻩ ﻏﻴﺮ ﻣﻬﻢ ﻓﻲ ﺗﻠﻚ اﻟﻌﻼﻗﺔ أي إن اﻟﺤﺎﻓﺔ )‪ (1,2‬هﻲ ﻧﻔﺴﻬﺎ )‪(2,1‬آﻤﺎ‬ ‫ﻓﻲ اﻟﻤﺜﺎل أﻋﻼﻩ‪.‬‬ ‫‪Directed Graph‬‬ ‫* ﻣﺨﻄﻂ ﻣﺘﺠﻪ‬ ‫هﻮ اﻟﻤﺨﻄﻂ اﻟﺬي ﺗﻜﻮن اﻟﻌﻼﻗﺔ ﺑﻴﻦ ﻋﻨﺎﺻﺮﻩ ﻣﺮﺗﺒﺔ أي ان اﻻﺗﺠﺎﻩ ﻣﻬﻢ ﻓﻲ ﺗﻠﻚ اﻟﻌﻼﻗﺔ أي إن اﻟﺤﺎﻓﺔ )‪ (1,2‬هﻲ ﻟﻴﺲ ﻧﻔﺴﻬﺎ )‪.(2,1‬‬ ‫ﻣﺜﺎل‪-:‬‬

‫‪1‬‬

‫‪3‬‬

‫‪6‬‬

‫‪2‬‬

‫‪4‬‬

‫‪5‬‬

‫‪1‬‬

‫*اﻟﻤﺴﺎر ‪Path‬‬ ‫هﻮ ﻣﺠﻤﻮﻋﺔ ﻣﻦ اﻟﻤﺴﺘﻘﻴﻤﺎت )اﻟﺨﻄﻮط( اﻟﺘﻲ ﺗﺮﺑﻂ ﺑﻴﻦ أي ﻧﻘﻄﺘﻴﻦ ﻓﻲ اﻟﻤﺨﻄﻂ أو هﻮ اﻟﻤﺴﺎرﻣﻦ ﻧﻘﻄﺔ اﻻﻧﻄﻼق اﻟﻰ ﻧﻘﻄﺔ اﻟﻬﺪف ‪.‬‬ ‫آﺬﻟﻚ ﺗﻘﺴﻢ اﻟﻤﺨﻄﻄﺎت إﻟﻰ‪-:‬‬ ‫*ﻣﺨﻄﻄﺎت ﻣﺘﺼﻠﺔ‬

‫‪Connected Graphics‬‬

‫وهﻮ اﻟﻤﺨﻄﻂ اﻟﺬي ﺗﻮﺟﺪ ﻓﻴﻪ ﻣﺴﺎرات ﺑﻴﻦ أي ﻧﻘﻄﺘﻴﻦ ﻣﻦ ﻧﻘﺎﻃﻪ )وﻳﻤﻜﻦ ان ﻳﻜﻮن ﻣﺘﺠﻪ او ﻏﻴﺮ ﻣﺘﺠﻪ( آﻤﺎ ﻓﻲ اﻟﻤﺜﺎﻟﻴﻦ اﻋﻼﻩ‪.‬‬ ‫* ﻣﺨﻄﻄﺎت ﻏﻴﺮ اﻟﻤﺘﺼﻠﺔ ‪Unconnected Graphics‬‬ ‫وهﻮ اﻟﻤﺨﻄﻂ اﻟﺬي ﺗﻮﺟﺪ ﻓﻴﻪ ﻧﻘﺎﻃﻪ ﻏﻴﺮ ﻣﺮﺗﺒﻄﺔ ﺑﻤﺴﺎرات‪.‬‬ ‫ﻣﺜﺎل‪-:‬‬

‫‪1‬‬

‫‪2‬‬

‫‪3‬‬

‫‪4‬‬

‫‪5‬‬

‫ﻣﺨﻄﻂ ﻏﻴﺮ ﻣﺘﺼﻞ‬

‫‪Graph Representation‬‬ ‫ﺗﻤﺜﻴﻞ اﻟﻤﺨﻄﻄﺎت‪ -:‬ان اﺧﺘﻴﺎر ﻃﺮﻳﻘﺔ ﺗﻤﺜﻴﻞ اﻟﻤﺨﻄﻂ ﺗﻌﺘﻤﺪ ﻋﻠﻰ ﻧﻮع اﻟﺘﻄﺒﻴﻖ اﻟﻤﻄﻠﻮب اﻧﺠﺎزﻩ وﻃﺒﻴﻌﺔ وﻇﺎﺋﻔﻪ وهﻨﺎﻟﻚ ﻃﺮﻳﻘﺘﻴﻦ ﻟﺘﻤﺜﻴﻞ‬ ‫اﻟﻤﺨﻄﻄﺎت هﻲ‪-:‬‬ ‫*اﺳﺘﺨﺪام ﻣﺼﻔﻮﻓﺔ اﻟﺘﺠﺎور ‪Adjancency Matrix‬‬ ‫هﻨﺎ ﻳﺘﻢ ﺗﻤﺜﻴﻞ اﻟﻤﺨﻄﻂ ﺑﻤﺼﻔﻮﻓﺔ ﻣﺮﺑﻌﺔ درﺟﺘﻬﺎ ﻣﺴﺎوﻳﺔ ﻟﻌﺪد ﻋﻨﺎﺻﺮ اﻟﻤﺨﻄﻂ ﻓﺎذا آﺎن اﻟﻤﺨﻄﻂ ﻳﺤﺘﻮي ﻋﻠﻰ ﺛﻼث ﻋﻨﺎﺻﺮ ﻓﺎﻧﻪ ﻳﻤﺜﻞ‬ ‫ﺑﻤﺼﻔﻮﻓﺔ )‪ (3*3‬اﻣﺎ اذا آﺎن ﻳﺤﺘﻮي ﻋﻠﻰ ارﺑﻌﺔ ﻋﻨﺎﺻﺮ ﻓﺎﻧﻪ ﻳﻤﺜﻞ ﺑﻤﺼﻔﻮﻓﺔ )‪ (4*4‬وهﻜﺬا‪.‬‬ ‫ﻣﺜﺎل‪ -:‬ﻳﻮﺿﺢ آﻴﻒ ﻳﻤﻜﻦ ﺗﻤﺜﻴﻞ ﻣﺨﻄﻂ ﻣﺘﺠﻪ ﺑﻤﺼﻔﻮﻓﺔ اﻟﺘﺠﺎور‬ ‫‪S1‬‬

‫‪S3‬‬

‫‪S4‬‬

‫‪S2‬‬

‫ﻳﻤﺜﻞ هﺬا اﻟﻤﺨﻄﻂ ﺑﻤﺼﻔﻮﻓﺔ ﻣﺮﺑﻌﺔ ﻣﻦ اﻟﺪرﺟﺔ )‪ (4‬وﻋﻨﺎﺻﺮهﺎ ) ‪ ( Sij‬ﺣﻴﺚ ‪ i‬ﻳﻤﺜﻞ ﻧﻘﻄﺔ اﻟﺒﺪاﻳﺔ و‬

‫‪ j‬ﻳﻤﺜﻞ ﻧﻘﻄﺔ اﻟﻨﻬﺎﻳﺔ‬

‫وﻓﻲ ﺣﺎﻟﺔ وﺟﻮد ﺣﺎﻓﺔ)ﻣﺴﺎر ( ﺑﻴﻦ ﻧﻘﻄﺘﻴﻦ ﻳﺘﻢ وﺿﻊ)واﺣﺪ( ﻟﻴﻤﺜﻞ اﻟﻤﻮﻗﻊ وﺑﻌﻜﺴﻪ ﻳﺘﻢ وﺿﻊ )ﺻﻔﺮ(‬ ‫‪4‬‬ ‫‪1‬‬ ‫‪0‬‬

‫ﻧﻘﺎط اﻟﻨﻬﺎﻳﺔ‬ ‫‪3‬‬ ‫‪0‬‬ ‫‪0‬‬

‫‪1‬‬ ‫‪0‬‬

‫‪1‬‬ ‫‪0‬‬ ‫‪0‬‬

‫‪2‬‬

‫‪1‬‬

‫‪0‬‬

‫‪1‬‬

‫‪0‬‬

‫‪3‬‬

‫‪0‬‬

‫‪1‬‬

‫‪0‬‬

‫‪0‬‬

‫‪4‬‬

‫‪2‬‬

‫‪2‬‬

‫‪ 1‬ﻧﻘﺎط اﻟﺒﺪاﻳﺔ‬

‫ﺧﺼﺎﺋﺺ اﻟﻤﺼﻔﻮﻓﺔ اﻟﺘﻲ ﺗﻤﺜﻞ اﻟﻤﺨﻄﻂ اﻟﻤﺘﺠﻪ‪-:‬‬ ‫‪ .1‬ﻣﺠﻤﻮع اﻟﻘﻴﻢ ﻓﻲ اﻟﻌﻤﻮد ﺗﻤﺜﻞ ﻋﺪد اﻟﺨﻄﻮط اﻟﺪاﺧﻠﺔ ﻟﺘﻠﻚ اﻟﻨﻘﻄﺔ‬ ‫‪ .2‬ﻣﺠﻤﻮع اﻟﻘﻴﻢ ﻓﻲ اﻟﺼﻒ ﺗﻤﺜﻞ ﻋﺪد اﻟﺨﻄﻮط اﻟﺨﺎرﺟﺔ ﻣﻦ داﺧﻠﺔ ﺗﻠﻚ اﻟﻨﻘﻄﺔ‬ ‫‪ .3‬اﻟﻤﺼﻔﻮﻓﺔ اﻟﻤﺴﺘﺨﺪﻣﺔ ﻟﺨﺰن اﻟﻤﺨﻄﻂ اﻟﻤﺘﺠﻪ ﺗﻤﺘﺎز ﺑﺎﻧﻬﺎ ﻏﻴﺮ ﻣﺘﻨﺎﻇﺮة‬ ‫ﻣﺜﺎل‪ -:‬ﻳﻮﺿﺢ آﻴﻒ ﻳﻤﻜﻦ ﺗﻤﺜﻴﻞ ﻣﺨﻄﻂ ﻏﻴﺮ ﻣﺘﺠﻪ ﺑﻤﺼﻔﻮﻓﺔ اﻟﺘﺠﺎور‬ ‫‪T1‬‬

‫‪T3‬‬

‫‪T2‬‬

‫‪T5‬‬

‫‪T4‬‬

‫ﻳﻤﺜﻞ هﺬا اﻟﻤﺨﻄﻂ ﺑﻤﺼﻔﻮﻓﺔ ﻣﺮﺑﻌﺔ ﻣﻦ اﻟﺪرﺟﺔ )‪ (5‬وﻋﻨﺎﺻﺮهﺎ ) ‪ ( Tij‬ﺣﻴﺚ ‪ i‬ﻳﻤﺜﻞ ﻧﻘﻄﺔ اﻟﺒﺪاﻳﺔ و‬

‫‪ j‬ﻳﻤﺜﻞ ﻧﻘﻄﺔ اﻟﻨﻬﺎﻳﺔ‬

‫وﻓﻲ ﺣﺎﻟﺔ وﺟﻮد ﺣﺎﻓﺔ)ﻣﺴﺎر ( ﺑﻴﻦ ﻧﻘﻄﺘﻴﻦ ﻳﺘﻢ وﺿﻊ)واﺣﺪ( ﻟﻴﻤﺜﻞ اﻟﻤﻮﻗﻊ وﺑﻌﻜﺴﻪ ﻳﺘﻢ وﺿﻊ )ﺻﻔﺮ(‬ ‫‪5‬‬

‫ﻧﻘﺎط اﻟﻨﻬﺎﻳﺔ‬ ‫‪3‬‬ ‫‪1‬‬ ‫‪1‬‬

‫‪2‬‬ ‫‪1‬‬ ‫‪0‬‬

‫‪1‬‬ ‫‪0‬‬ ‫‪1‬‬

‫‪4‬‬ ‫‪0‬‬ ‫‪0‬‬

‫‪1‬‬

‫‪0‬‬

‫‪0‬‬

‫‪1‬‬

‫‪1‬‬

‫‪3‬‬

‫‪1‬‬

‫‪0‬‬

‫‪0‬‬

‫‪0‬‬

‫‪0‬‬

‫‪0‬‬

‫‪1‬‬

‫‪1‬‬

‫‪0‬‬

‫‪1‬‬

‫‪4‬‬ ‫‪5‬‬

‫‪1‬‬ ‫‪0‬‬

‫‪ 1‬ﻧﻘﺎط اﻟﺒﺪاﻳﺔ‬ ‫‪2‬‬

‫ﺧﺼﺎﺋﺺ اﻟﻤﺼﻔﻮﻓﺔ اﻟﺘﻲ ﺗﻤﺜﻞ اﻟﻤﺨﻄﻂ ﻏﻴﺮ اﻟﻤﺘﺠﻪ‪-:‬‬ ‫‪ .1‬ﻣﺠﻤﻮع اﻟﻘﻴﻢ ﻓﻲ اﻟﻌﻤﻮد ﺗﻤﺜﻞ ﻋﺪد اﻟﺨﻄﻮط اﻟﺪاﺧﻠﺔ ﻟﺘﻠﻚ اﻟﻨﻘﻄﺔ‬ ‫‪ .2‬ﻣﺠﻤﻮع اﻟﻘﻴﻢ ﻓﻲ اﻟﺼﻒ ﺗﻤﺜﻞ ﻋﺪد اﻟﺨﻄﻮط اﻟﺨﺎرﺟﺔ ﻣﻦ داﺧﻠﺔ ﺗﻠﻚ اﻟﻨﻘﻄﺔ‬ ‫‪ .3‬اﻟﻤﺼﻔﻮﻓﺔ اﻟﻤﺴﺘﺨﺪﻣﺔ ﻟﺨﺰن اﻟﻤﺨﻄﻂ ﻏﻴﺮ اﻟﻤﺘﺠﻪ ﺗﻤﺘﺎز ﺑﺎﻧﻬﺎ ﻣﺘﻨﺎﻇﺮة ﺣﻮل اﻟﻤﺤﻮر )اﻟﻤﺜﻠﺚ اﻻﻋﻠﻰ ﻳﻨﺎﻇﺮ اﻟﻤﺜﻠﺚ اﻻﺳﻔﻞ( وﺑﺬﻟﻚ‬ ‫ﻳﻤﻜﻦ اﺧﺘﺼﺎر ﻧﺼﻒ اﻟﻤﺴﺎﺣﺔ اﻟﺨﺰﻧﻴﺔ ﻣﻦ ﺧﻼل ﺗﻤﺜﻴﻞ اﺣﺪ اﻟﻤﺜﻠﺜﻴﻦ ﻓﻘﻂ‪.‬‬ ‫*اﺳﺘﺨﺪام ﻗﻮاﺋﻢ اﻟﺘﺠﺎور‬

‫‪Adjancency Lists‬‬

‫‪.‬‬ ‫‪Linked‬‬ ‫ﺗﺴﺘﺨﺪم اﻟﻘﻮاﺋﻢ اﻟﻤﻮﺻﻮﻟﺔ ‪List‬‬

‫ﻟﺘﻤﺜﻴﻞ اﻟﻤﺨﻄﻂ ﺣﻴﺚ ان آﻞ ﻋﻘﺪة ﻣﻦ ﻋﻘﺪ اﻟﻤﺨﻄﻂ ﺗﻤﺜﻞ ﺑﻘﺎﺋﻤﺔ ﻣﺘﺼﻠﺔ ﺗﺤﺘﻮي اﺳﻤﺎء اﻟﻌﻘﺪ‬

‫اﻟﺘﻲ ﺗﺘﺼﻞ ﺑﻬﺎ ‪.‬‬

‫‪3‬‬

‫ ﻳﻮﺿﺢ آﻴﻒ ﻳﻤﻜﻦ ﺗﻤﺜﻴﻞ ﻣﺨﻄﻂ ﻏﻴﺮ ﻣﺘﺠﻪ ﺑﻘﻮاﺋﻢ اﻟﺘﺠﺎور‬-:‫ﻣﺜﺎل‬ 1

3

2

4

2* ‫ﻋﺪد اﻟﻌﻘﺪ اﻟﺘﻲ ﻳﻤﻜﻦ ان أﻣﺜﻠﻬﺎ ﻟﺬﻟﻚ اﻟﻤﺨﻄﻂ= ﻋﺪد اﻟﺤﻮاف‬ (No of nodes=No of edges*2) -:‫( ﻟﺬﻟﻚ ﻳﻤﻜﻦ ان ﻳﻤﺜﻞ ﺑﻘﻮاﺋﻢ اﻟﻤﺘﺠﺎورة آﻤﺎ ﻳﺄﺗﻲ‬1,2,3,4)‫هﺬا اﻟﻤﺨﻄﻂ ﻳﺤﺘﻮي ﻋﻠﻰ ارﺑﻌﺔ رؤس هﻲ‬ Vertex1

2

3

4

null

1

3

4

null

1

2

4

null

1

2

3

null

Vertex2 Vertex3 Vertex4

-:‫اﻟﺘﻌﺮﻳﻒ اﻟﺒﺮﻣﺠﻲ‬ Public Type ptr vertex as integer {or any other type} Link as variant End type Public Type pointer vertex as integer {or any other type} Link as ptr End type Public heads(1 to n) as pointer ‫ ﻳﻮﺿﺢ آﻴﻒ ﻳﻤﻜﻦ ﺗﻤﺜﻴﻞ ﻣﺨﻄﻂ ﻣﺘﺠﻪ ﺑﻘﻮاﺋﻢ اﻟﺘﺠﺎور‬-:‫ﻣﺜﺎل‬ 2

3

1

4

4

‫ﻋﺪد اﻟﻌﻘﺪ اﻟﺘﻲ ﻳﻤﻜﻦ ان أﻣﺜﻠﻬﺎ ﻟﺬﻟﻚ اﻟﻤﺨﻄﻂ= ﻋﺪد اﻟﺤﻮاف‬ ‫)‪(No of nodes=No of edges‬‬ ‫هﺬا اﻟﻤﺨﻄﻂ ﻳﺤﺘﻮي ﻋﻠﻰ ارﺑﻌﺔ رؤس هﻲ)‪ (1,2,3,4‬ﻟﺬﻟﻚ ﻳﻤﻜﻦ ان ﻳﻤﺜﻞ ﺑﻘﻮاﺋﻢ اﻟﻤﺘﺠﺎورة آﻤﺎ ﻳﺄﺗﻲ‪-:‬‬ ‫‪null‬‬ ‫‪3‬‬

‫‪null‬‬

‫‪5‬‬

‫‪4‬‬ ‫‪1‬‬

‫‪Vertex1‬‬ ‫‪Vertex 2‬‬

‫‪null‬‬

‫‪2‬‬

‫‪Vertex3‬‬

‫‪null‬‬

‫‪1‬‬

‫‪Vertex 4‬‬

Related Documents

Lecture 7
November 2019 33
Lecture 7
April 2020 18
Lecture 7
June 2020 6
Lecture 7
April 2020 6
Lecture 7
April 2020 6
Lecture 7
October 2019 28