Autoria: Carlos Eduardo Cayres | 4o

17 

Full text

(1)

Estrutura de Dados

Autoria: Carlos Eduardo Cayres | 4

o

semestre

Tema 08

Grafos e suas Aplicações

Tema 08

Grafos e suas Aplicações

Autoria: Carlos Eduardo Cayres

Como citar esse documento:

CAYRES, Carlos Eduardo. Estrutura de Dados: Grafos e suas Aplicações. Caderno de Atividades. Anhanguera Educacional: Valinhos, 2014.

Índice

‹ $QKDQJXHUD (GXFDFLRQDO 3URLELGD D UHSURGXomR ¿QDO RX SDUFLDO SRU TXDOTXHU PHLR GH LPSUHVVmR HP IRUPD LGrQWLFD UHVXPLGD RX PRGL¿FDGD HP OtQJXD SRUWXJXHVDRXTXDOTXHURXWURLGLRPD

Pág. 30

Pág. 31 Pág. 31

Pág. 30 Pág. 27 Pág. 26

ACOMPANHE

NA

WEB

Pág. 3

CONVITE

À

LEITURA

(2)

3

Grafos e suas Aplicações

8PDGH¿QLomRVLPSOHVGHJUDIRVHULDDVHJXLQWHXPFRQMXQWRGHYpUWLFHVRXQyVHXPFRQMXQWRGHarestasRX

DUFRVVHQGRTXHFDGDDUHVWDHPXPJUDIRpUHSUHVHQWDGDSRUXPSDUGHYpUWLFHV8PJUDIR* 9$FRQVLVWHHP &RQMXQWR¿QLWRGHvértices97DLVHOHPHQWRVGH9VmRFKDPDGRVYpUWLFHVGRJUDIR*

&RQMXQWR¿QLWR$GHSDUHVQmRRUGHQDGRVGH97DLVHOHPHQWRVGH$VmRFKDPDGRVDUHVWDVGRJUDIR*

(VWHWHPDDSUHVHQWDDHVWUXWXUDGHGDGRVGRWLSRJUDIRVXPDGDVHVWUXWXUDVGHSURJUDPDomRPDLVXWLOL]DGDVHP SURJUDPDomR

*UDIRSRGHVHUEDVLFDPHQWHGH¿QLGRFRPRXPFRQMXQWRGHYpUWLFHVRXQyVHXPFRQMXQWRGHDUHVWDVRXDUFRVVHQGR TXHFDGDDUHVWDHPXPJUDIRpUHSUHVHQWDGDSRUXPSDUGHYpUWLFHV

'LIHU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GHdirected acyclic graph7(1(1%$80/$1*6$0$8*(167(,1

Aplicações com Grafos

Busca em profundidade

1RVDOJRULWPRVGHEXVFDHPSURIXQGLGDGHDVDUHVWDVVmRSHUFRUULGDVSDUWLQGRGRYpUWLFHYTXHWHQKDVLGRGHVFREHUWR SRU~OWLPRHTXHWHQKDDUHVWDVQmRGHVFREHUWDVDSDUWLUGHOH

'HSRLVTXHWRGDVDVDUHVWDVSDUWLQGRGHYIRUHPGHVFREHUWDVXPDQRYDEXVFDVHLQLFLDSDUDSHUFRUUHUDVDUHVWDVSDUWLQGR GRYpUWLFHDSDUWLUGRTXDOYIRLGHVFREHUWR8PEXVFDHPSURIXQGLGDGHSRGHUiJHUDUDOJXPDViUYRUHVGHSHQGHQGRGR Q~PHURGHRULJHQVDSDUWLUGDVTXDLVDEXVFDpUHSHWLGD

1DEXVFDHPSURIXQGLGDGHDVVLPFRPRQDEXVFDHPODUJXUDRVYpUWLFHVVmRFRORULGRVLQGLFDQGRVHXHVWDGR

Exemplo 1 - Busca em profundidade

LQFOXGHLRVWUHDP! LQFOXGHPDS! LQFOXGHOLVW! LQFOXGHVWULQJ! XVLQJQDPHVSDFHVWG FRQVWVWULQJ3 ³SUHWR´ FRQVWVWULQJ% ³EUDQFR´ FRQVWVWULQJ& ³FLQ]D´

W\SHGHIOLVWLQW!OLVWD$GMDFHQFLDV W\SHGHIPDSLQWOLVWD$GMDFHQFLDV!JUDIR PDSLQWVWULQJ!FRU

OLVWLQW!RUGHP7RSRORJLFD YRLGYLVLWDU1RGRJUDIR*LQWX ^

FRU>X@ &

IRUOLVWD$GMDFHQFLDVLWHUDWRUL *>X@EHJLQL *>X@HQGL

^

LIFRU>L@ %

YLVLWDU1RGR*L

`

FRU>X@ 3

RUGHP7RSRORJLFDSXVKBEDFNX

`

(4)

7

YRLGEXVFD3URIXQGLGDGHJUDIR* ^

IRUJUDIRLWHUDWRUL *EHJLQL *HQGL

FRU>L!¿UVW@ %

IRUJUDIRLWHUDWRUL *EHJLQL *HQGL

^

LIFRU>L!¿UVW@ % ^

YLVLWDU1RGR*L!¿UVW

`

`

`

LQWWHP&LFORJUDIR* ^

OLVWLQW!LWHUDWRULM

EXVFD3URIXQGLGDGH*

IRUL RUGHP7RSRORJLFDEHJLQL RUGHP7RSRORJLFDHQGL ^

M L

IRUMM RUGHP7RSRORJLFDHQGM

LI¿QG*>L@EHJLQ*>L@HQGM *>L@HQG

UHWXUQ

`

UHWXUQ

`

LQWPDLQLQWDUJFFKDUDUJY>@ ^

),/(DUTXLYR

JUDIR*

LQWQRDUHVWD

LIDUTXLYR IRSHQ³F??JUDIRV??DUTXLYRW[W´³U´ 18//

^

SULQWI³?Q?Q(UURQDOHLWXUDGRDUTXLYR?Q´

H[LW

`

ZKLOHIHRIDUTXLYR

^

IVFDQIDUTXLYR´GG´ QR DUHVWD

LI*>QR@HPSW\

*>QR@ OLVWD$GMDFHQFLDV

LI*>DUHVWD@HPSW\

*>DUHVWD@ OLVWD$GMDFHQFLDV *>QR@SXVKBEDFNDUHVWD

`

(5)

9

IFORVHDUTXLYR

IRUJUDIRLWHUDWRUL *EHJLQL *HQGL

LIL!VHFRQGHPSW\

L!VHFRQGVRUW

LIWHP&LFOR* SULQWI³*UDIRDFLFOLFR?Q´

HOVH

SULQWI³+DFLFORVQRJUDIR?Q´

V\VWHP³3$86(´

UHWXUQ `

Arquivo para teste – arquivo.txt

$QWHVGHH[HFXWDURSURJUDPDDQWHULRUPHQWHGHVFULWRGHYHVHFULDURDUTXLYRDVHJXLUQDSDVWDc:\grafos\arquivo.txt

Lista de adjacências

8PDOLVWDGHDGMDFrQFLDVHPJUDIRVSRGHVHUGH¿QLGDFRPRDUHSUHVHQWDomRGHWRGDVDVDUHVWDVRXDUFRVGHXP JUDIRHPXPDOLVWD

(PJUDIRVQmRGLUHFLRQDGRVDVHQWUDGDVVmRFRQMXQWRVGHGRLVQyVFRQWHQGRDVH[WUHPLGDGHVGDDUHVWDFRUUHVSRQGHQWH 1RFDVRGHJUDIRVGLULJLGRVDHQWUDGDpXPDWXSODGHGRLVQyVUHSUHVHQWDQGRRULJHPHGHVWLQRGHXPDUFRTXDOTXHU 1DPDLRULDGDVUHSUHVHQWDo}HVDVOLVWDVGHDGMDFrQFLDVQmRVmRRUGHQDGDV

Exemplo 2 - Lista de adjacências

LQFOXGHLRVWUHDP! LQFOXGHVWGLRK! GH¿QH727B9(57

W\SHGHIVWUXFWLWHQV^

LQWFPS

VWUXFWLWHQVSUR[ `LWHQV

LWHQVOLVWD>727B9(57@

YRLG/LVWDULWHQVOLVWD

YRLG,QVHUHLWHQVOLVWDLQWDLQWE

LQWPDLQLQWDUJFFKDUDUJY>@^ LQWLDE

),/(IS

(6)

11

IS IRSHQ³F??JUDIRV??HQWUDGDW[W´´U´

LIIS^

SULQWI³(UURQDWHQWDWLYDGHDEULURDUTXLYRV?Q´

UHWXUQ

`

IRUL L 727B9(57L^

OLVWD>L@FPS

OLVWD>L@SUR[ 18// `

IVFDQIIS´GG´ D E ZKLOHIHRIIS^

,QVHUHOLVWDDE ,QVHUHOLVWDED

IVFDQIIS´GG´ D E `

/LVWDUOLVWD V\VWHP³3$86(´ `

YRLG/LVWDULWHQVOLVWD^ LQWL

LWHQV7HPSRUDULD IRUL L 727B9(57L^

7HPSRUDULD OLVWD>L@SUR[

SULQWI³GG !´LOLVWD>L@FPS ZKLOH7HPSRUDULD 18//^ SULQWI³G³7HPSRUDULD!FPS 7HPSRUDULD 7HPSRUDULD!SUR[ `

SULQWI³?Q´ `

`

YRLG,QVHUHLWHQVOLVWDLQWDLQWE^ LWHQVDX[

LWHQV7HPSRUDULD

DX[ LWHQVPDOORFLQWVL]HRILWHQV DX[!FPS E

DX[!SUR[ 18//

OLVWD>D@FPS LIOLVWD>D@SUR[ 18// OLVWD>D@SUR[ DX[ HOVH^

7HPSRUDULD OLVWD>D@SUR[ LI7HPSRUDULD!FPS!E^ DX[!SUR[ 7HPSRUDULD OLVWD>D@SUR[ DX[

`

(7)

13

HOVHL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[HFXomR(QWUHWDQWRFDVRH[LVWDPDUFRVFRPYDORUHVQHJDWLYRVRDOJRULWPRQmR JDUDQWHH[DWLGmRGDVROXomR

1DEXVFDGRFXVWRPtQLPRRDOJRULWPRSDUWHGHXPDHVWLPDWLYDLQLFLDOHYDLDMXVWDQGRHVWDHVWLPDWLYD

Exemplo 3 – Menor caminho LQFOXGHVWGLRK!

LQFOXGHVWGOLEK! GH¿QH',0 9DULiYHLVJOREDLV

LQW*UDIR>',0@>',0@*UDIR>',0@>',0@0BF>',0@>',0@0BS>',0@>',0@&RQHFWLYLGDGH>',0@>',0@RSRULGHV YRLG&DUUH*UDIRDULQWP>',0@>',0@LQWY

^ LQWLM L M ZKLOHL ^

ZKLOHM ^

(8)

15

P>L@>M@ Y

M

` M L ` `

YRLG0RVWUDULQWP>',0@>',0@ ^

LQWLMN V\VWHP³&/6´ L M N SULQWI³³ ZKLOHN

^

SULQWI³G³N N

`

SULQWI³?Q?Q?Q³

N

ZKLOHL ^

SULQWI³G³N ZKLOHM ^

SULQWI³G³P>L@>M@

M

` L M N

SULQWI³?Q?Q³

`

`

YRLG0RVWUDUBLQWP>',0@>',0@ ^

LQWLMN L M N

V\VWHP³&/6´ SULQWI³³ ZKLOHN ^

SULQWI³G³N N

` N

SULQWI³?Q?Q?Q´

ZKLOHL

^

SULQWI³G³N

(9)

17

ZKLOHM

^

SULQWI³G³P>L@>M@ M

` L M N SULQWI³?Q?Q´

`

`

YRLG0BFRQHFWDULQWP>',0@>',0@LQWDLQWELQWY ^

P>D@>E@ Y `

LQW0BDGMDFHQWHLQWP>',0@>',0@LQWDLQWE ^

LIP>D@>E@ ^

UHWXUQ ` HOVH ^ UHWXUQ `

`

YRLG0BFXVWRLQW*UDIR>',0@>',0@LQWF>',0@>',0@LQWS>',0@>',0@ ^

LQWLMN L M N ZKLOHL ^

ZKLOHM

^

LI*UDIR>L@>M@ ^

F>L@>M@ *UDIR>L@>M@

`

HOVH ^ F>L@>M@ ` M ` L M ` L ZKLOHL ^ F>L@>L@

(10)

19

L ` L M N ZKLOHN ^

ZKLOHL ^

ZKLOHM ^

LIF>L@>N@F>N@>M@F>L@>M@

^

F>L@>M@ F>L@>N@F>N@>M@ S>L@>M@ N

` M ` L M ` N L ` `

YRLG0RVWUDUBFDPLQKRLQWP>',0@>',0@LQWDLQWE ^

LQWNGU N P>D@>E@ LIN

^

0RVWUDUBFDPLQKRPDN SULQWI³G !³N

0RVWUDUBFDPLQKRPNE `

`

YRLG0BFRQHFWLYLGDGHLQWP>',0@>',0@LQWFG>',0@>',0@ ^

LQWLMN

IRUL L L ^

IRUM M M ^

FG>L@>M@ P>L@>M@ `

`

IRUN N N ^

IRUL L L ^

IRUM M M ^

LIFG>L@>M@

(11)

21

^

FG>L@>M@ FG>L@>N@FG>N@>M@

`

` ` ` `

LQW0RVWUDUBFHQWURLQWP>',0@>',0@ ^

LQWLM[YHWRU>',0@

L

M ZKLOHL ^

YHWRU>L@ L ` L ZKLOHM ^

ZKLOHL ^

LIP>L@>M@!YHWRU>M@

^

YHWRU>M@ P>L@>M@ `

L

`

M L ` [ L ZKLOHL

^

LIYHWRU>L@[ ^

[ YHWRU>L@ `

L ` UHWXUQ[ `

YRLG0RQWDUBPHQXLQW*UDIR>',0@>',0@LQW0BF>',0@>',0@LQW0BS>',0@>',0@LQW&RQHFWLYLGDGH>',0@>',0@ ^

GR^

V\VWHP³&/6´

SULQWI³0RVWUDUDPDWUL]GHDGMDFHQFLDGRJUDIR?Q´ SULQWI³0RVWUDUDPDWUL]GHPHQRUHVFXVWRVGRJUDIR?Q´ SULQWI³0RVWUDUDPDWUL]GHFRQHFWLYLGDGHGRJUDIR?Q´ SULQWI³0RVWUDURFDPLQKR?Q´

SULQWI³6DLU´

(12)

23

SULQWI³?Q'LJLWHDVXDRS³

VFDQI³G´ RS

`ZKLOHRS__RS!

LIRS

^

0RVWUDU*UDIR V\VWHP³3$86(´

0RQWDUBPHQX*UDIR0BF0BS&RQHFWLYLGDGH

`

LIRS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` LQWPDLQ ^

V\VWHP³&/6´ &DUUH*UDIRDU*UDIR &DUUH*UDIRDU0BF &DUUH*UDIRDU0BS &DUUH*UDIRDU*UDIR &DUUH*UDIRDU&RQHFWLYLGDGH ,QLFLDOL]DUBJUDIR

0BFXVWR*UDIR0BF0BS

0BFRQHFWLYLGDGH*UDIR&RQHFWLYLGDGH 0RQWDUBPHQX*UDIR0BF0BS&RQHFWLYLGDGH `

POR

DENTRO

DO

TEMA

Grafos I: Conceitos e Aplicações

‡ ,QWURGXomRDRVJUDIRVGH¿QLomRWHUPLQRORJLDDOJXPDVSURSULHGDGHVHH[HPSORVGHDSOLFDo}HV GHJUDIRV

'LVSRQtYHOHPKWWSZLNLLFPFXVSEULPDJHV*UDIRVB,SGI!$FHVVRHPDJR

`

Grafos I: Conceitos e Aplicações

(14)

27

Algoritmos em Grafos

‡ 6XSRQKDTXHH[LVWDPVHLVVLVWHPDVFRPSXWDFLRQDLV$%&'(H)LQWHUFRQHFWDGRVHQWUH VL(VWDLQIRUPDomRSRGHVHUUHSUHVHQWDGDSRUXPGLDJUDPDFKDPDGRGHJUDIR

'LVSRQtYHOHPKWWSKRPHSDJHVGFFXIPJEUaORXUHLURDOJSDHBFDSSGI!$FHVVRHPDJR

Grafos - Estrutura de Dados 2

‡ *UDIRVpXPWLSRDEVWUDWRGHHVWUXWXUDGHGDGRVHPTXHGDGRXPQ~PHURGHREMHWRVKiXP FRQMXQWRGHFRQH[}HVHQWUHSDUHVGHVWHVREMHWRV

'LVSRQtYHOHPKWWSZZZ\RXWXEHFRPZDWFK"Y BXN(K.N!$FHVVRHPDJR 7HPSR

Instruções:

$JRUDFKHJRXDVXDYH]GHH[HUFLWDUVHXDSUHQGL]DGR$VHJXLUYRFrHQFRQWUDUiDOJXPDVTXHVW}HVGHP~OWLSOD HVFROKDHGLVVHUWDWLYDV/HLDFXLGDGRVDPHQWHRVHQXQFLDGRVHDWHQWHVHSDUDRTXHHVWiVHQGRSHGLGR

7HPSR

AGORA

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RQy"6HDUHVSRVWDIRUVLP GHVFUHYDRVFDPLQKRV

3

1 4

a1 a2 a3

a4

a5

a6

2

5

6

Questão 5

2EVHUYHRJUDIRDVHJXLU([LVWHDOJXPFLFORQRJUDIR"6HDUHVSRVWDIRUVLPGHVFUHYDRVFDPLQKRVSHUFRUULGRVSHORVFLFORV

3

1 4

a1 a2 a3

a4

a5

a6

2

5

6

2VFRQFHLWRVHH[HPSORVSUiWLFRVHP&DSUHVHQWDGRVQHVWHWHPDGHL[DUDPEHPFODURTXHRVJUDIRVVmRPXLWR YHUViWHLVQDVROXomRGHSUREOHPDVGRFRWLGLDQR

'LIHUHQWHPHQWHGHRXWURVUDPRVGDPDWHPiWLFDTXHPXLWDVYH]HVDSUHVHQWDPWUDEDOKRVFRPEDVHVRPHQWHWHyULFDD WHRULDGRVJUDIRVpIRFDGDHPSUREOHPDVSUiWLFRVGRFRWLGLDQR6HXHPEDVDPHQWRHVWiGLUHFLRQDGRDRHVWXGRGHREMHWRV FRPELQDWyULRV JUDIRV TXH UHSUHVHQWDP PRGHORV SDUD SUREOHPDV HP GLYHUVRV UDPRV GD PDWHPiWLFD FRPSXWDomR HQJHQKDULDLQG~VWULDHQWUHRXWURV

)RUDPDSUHVHQWDGRVFRQFHLWRVEiVLFRVVREUHJUDIRVHVHXXVRHP&FRPDOJXQVH[HPSORV$R¿QDOL]DUWLYHPRVDOJXQV H[HUFtFLRVSDUDUHIRUoDURFRQWH~GRHVWXGDGRUHIHUHQWHDRHVWXGRGRVJUDIRV

2VFRQFHLWRVHH[HPSORVSUiWLFRVHP&DSUHVHQWDGRVQHVWHWHPDGHL[DUDPEHPFODURTXHRVJUDIRVVmRPXLWR

FINALIZANDO

&$03(//25LFDUGR-*%*UDIRV,&RQFHLWRVH$SOLFDo}HVEstruturas de Dados'LVSRQtYHOHPKWWSZLNLLFPFXVSEU

LPDJHV*UDIRVB,SGI!$FHVVRHPMXQ

/825(,52$QWRQLR$OIUHGR)HUUHLUDAlgoritmos e Estruturas de Dados II$OJRULWPRVHP*UDIRV8)0*'LVSRQtYHOHP

KWWSKRPHSDJHVGFFXIPJEUaORXUHLURDOJSDHBFDSSGI!$FHVVRHPMXQ

6,/9$/XFLDQRGD6,/9(,5$$UWXU5DIDHOGD*UDIRV(VWUXWXUDGH'DGRVVídeo/YouTube'LVSRQtYHOHPKWWSZZZ\RXWXEH

FRPZDWFK"Y BXN(K.N!$FHVVRHPMXQ

7(1(1%$80$DURQ0/$1*6$0<HGLG\DK$8*(167(,10RVKH-Estruturas de Dados usando C6mR3DXOR0DNURQ

%RRNV

H[HUFtFLRVSDUDUHIRUoDURFRQWH~GRHVWXGDGRUHIHUHQWHDRHVWXGRGRVJUDIRV

(16)

31

Estrutura de dados:QDFRPSXWDomRHVWUXWXUDGHGDGRVpXPDIRUPDHVSHFt¿FDGHRUJDQL]DomRHDUPD]HQDPHQWR

GHGDGRVSDUDTXHVHMDPXWLOL]DGRVGHIRUPDH¿FD]$VHVWUXWXUDVGHGDGRVHVHXVDOJRULWPRVVmRPXLWRXWLOL]DGRVQD &LrQFLDGD&RPSXWDomRHPGLYHUVDViUHDVGRFRQKHFLPHQWRHFRPDVPDLVGLIHUHQWHV¿QDOLGDGHVQDVROXomRGHSUR EOHPDVFRPSXWDFLRQDLV

Aresta:QDJHRPHWULDRHQFRQWURGHGXDVIDFHVUHVXOWDHPOLQKDVFKDPDGDVGHDUHVWDV$DUHVWDWDPEpPpFKDPDGD

GH³UHWD´

Vértice:pRSRQWRFRPXPDGXDVRXPDLVUHWDVWDPEpPGH¿QLGRFRPRSRQWRGHOLJDomRHQWUHDVDUHVWDV2VYpUWLFHV

VmRJHUDOPHQWHUHSUHVHQWDGRVSRUXPSRQWR

Estrutura de dados:QDFRPSXWDomRHVWUXWXUDGHGDGRV pXPDIRUPDHVSHFt¿FDGHRUJDQL]DomRHDUPD]HQDPHQWR

VmRJHUDOPHQWHUHSUHVHQWDGRVSRUXPSRQWR

GABARITO

Questão 1

Resposta: 8PDiUYRUH%GHRUGHPSRGHWHUQRPi[LPRFKDYHVSRUQyIROKDHDUPD]HQDUQRPtQLPR17FKDYHV

SRUQy

3RUGH¿QLomRHPXPDiUYRUH%GHRUGHPPRQ~PHURPi[LPRGHFKDYHVHPXPQyIROKDpPHRQ~PHURPtQLPRGH FKDYHVHPXPQypGDGRSRUªm / 2º

Questão 2

Resposta: $OWHUQDWLYD$

8PDGH¿QLomRVLPSOHVGHJUDIRVHULDDVHJXLQWHXPFRQMXQWRGHYpUWLFHVRXQyVHXPFRQMXQWRGHDUHVWDVRXDUFRV VHQGRTXHFDGDDUHVWDHPXPJUDIRpUHSUHVHQWDGDSRUXPSDUGHYpUWLFHV8PJUDIR* 9$FRQVLVWHHP

GABARITO

&RQMXQWR¿QLWRGHYpUWLFHV97DLVHOHPHQWRVGH9VmRFKDPDGRVYpUWLFHVGRJUDIR*

&RQMXQWR¿QLWR$GHSDUHVQmRRUGHQDGRVGH97DLVHOHPHQWRVGH$VmRFKDPDGRVDUHVWDVGRJUDIR*

'L]HPRVTXHXPQy[TXDOTXHULQFLGHQRDUFR\VRPHQWHVH[UHSUHVHQWDUQRSDURUGHQDGRGHQyVTXHIRUPD\XPGRV GRLVQyV1HVWHFDVRSRGHPRVGL]HUTXH\LQFLGHHP[2Q~PHURGHDUFRVTXHLQFLGHPHPXPQyGHWHUPLQDRVHXJUDX 3RGHVHREVHUYDUWDPEpPTXH

3. O número de arcos que um nó tem como cabeça representa seu grau de entrada.

2Q~PHURGHDUFRVTXHXPQyWHPFRPRWHUPLQDomRGHVHWDUHSUHVHQWDVHXJUDXGHVDtGD

&RQIRUPHSRGHVHUREVHUYDGRQDVGH¿QLo}HVDQWHULRUPHQWHGHVFULWDVUHIHUHQWHVDRVFRQFHLWRVGHJUDIRVRLWHPHP QHJULWRFRUUHVSRQGHjUHVSRVWDGDTXHVWmR

Questão 3

Resposta: $OWHUQDWLYD&

$LPSOHPHQWDomRGHJUDIRVHPOLQJXDJHQVGHSURJUDPDomRH[LJHHVWUXWXUDVGHGDGRVPXOWLGLPHQVLRQDLVSDUDUHSUHVHQWi ORV,VVRVLJQL¿FDTXHpQHFHVViULRXWLOL]DUXPDHVWUXWXUDGHGDGRVTXHSHUPLWDRDUPD]HQDPHQWRGDVLQIRUPDo}HVGH IRUPDPXOWLGLPHQVLRQDO1RFDVRGDPDWUL]LVVRpSRVVtYHODWUDYpVGDHVWUXWXUDGHLQGH[DomRSRUOLQKDVHFROXQDV TXHSHUPLWHDLPSOHPHQWDomRGHXPDHVWUXWXUDPXOWLGLPHQVLRQDO7DOHVWUXWXUDDSOLFDGDQDUHSUHVHQWDomRGHJUDIRVp FKDPDGDGHPDWUL]GHDGMDFrQFLD

Questão 4

Resposta:6LP([LVWHPGRLVFDPLQKRVGRQySDUDRQy

1º caminho: ±D±±D±

2º caminho:±D±±D±±D±

Questão 5

(17)

Figure

Updating...

References

Updating...

Download now (17 página)