Agraph - - PDF document

a graph g v e consists of an set v of vertices
SMART_READER_LITE
LIVE PREVIEW

Agraph - - PDF document

Agraph G=(V,E)consistsofansetVofVERTICES andasetEofedges,withE={(u,v):u,v V,u v


slide-1
SLIDE 1
  • Agraph G=(V,E)consistsofansetVofVERTICES

andasetEofedges,withE={(u,v):u,v∈V,u≠ v Atree isaconnectedgraphwithnocycles. ∃ apathbetweeneachpairofvertices.

  • WhatisaTree
  • Abstractmodelofa

hierarchicalstructure

  • Atreeconsistsof

nodeswithaparent) childrelation

  • Applications:

– Organizationcharts – Filesystems – Programming environments

  • !"#
  • "

#$ $ %& '# ($ )#*

  • +

TreeTerminology

  • Root:nodewithoutparent(A)
  • (
  • ,
  • !

.

  • Descendant ofanode:child,grandchild,grand)grandchild,etc.
  • Internalnode:nodewithatleast
  • nechild(A,B,C,F)
  • Ancestors ofanode:parent,

grandparent,grand)grandparent,etc.

  • Externalnode (a.k.a.leaf ):

nodewithoutchildren(E,I,J,K,G,H,D)

  • Subtree:treeconsisting
  • fanodeandits

descendants

  • Depth ofanode:numberof

ancestors(=distancefromtheroot)

  • Height ofatree:maximumdepthof

anynode(3)

TreeTerminology

  • (
  • ,
  • !

.

Distancebetweentwonodes:number

  • f“edges” betweenthem
slide-2
SLIDE 2
  • genericcontainermethods

) size(),isEmpty(),elements()

  • positionalcontainermethods

) positions(),swapElements(p,q),replaceElement(p,e)

  • querymethods

) isRoot(p),isInternal(p),isExternal(p)

  • accessor methods

) root(),parent(p),children(p)

  • updatemethods

) applicationspecific

  • Algorithmdepth(T,v)

ifT.isRoot(v)then return0 else return1+depth(T,T.parent(v))

  • Ifvistherootthedepthis0

Ifvisaninternalnodethedepthis1+thedepthofitsparent Complexity?

  • Atraversalvisitsthenodesof

atreeinasystematicmanner

  • Inapreordertraversal,a
  • Application:printastructured

document

/-0 123

  • %2&

%21

  • %2% 4

& 121, 12%3/ %25( ++/

1 % 5 6 7 8 9 : ;

  • DBACFEHLIG
  • Inapostordertraversal,a
  • Application:computespace

usedbyfilesinadirectory anditssubdirectories

  • 18<

&=< 2> 1. < 2?3 1@. 2?3 %6. &12 5. &12 %. +2?3 %@.

; 5 1 9 % 7 6 8 :

  • ! "

ACBFLHIGED

slide-3
SLIDE 3
  • Letd(x)bethenumber
  • fsub)treesofnodex.

Start:x=root IN)ORDERVISIT 1. Visitthefirstsub)tree(inorder) 2. Visittheroot 3. Visitthesecondsub)tree(inorder) M M d(x)+1. Visitthed(x)th sub)tree(inorder)

Inorder !"

ABCDFLHEIG

  • "

#$ $ ' # ($ )#*

PhilipIII, Charles,

WhenCharlesdies,PhilipIIbecomesKing. IfPhilipIIdiesaswell….

PhilipII, CharlesI, RudolphII,Ernest,Mathias,Max,Albert,Wenzel,

  • !"!#$

rightchild leftchild

#

  • $% #$%"

isaleaf,or hastwochildren

{

Eachnode: Fullbinarytreeswithallleavesatthesame level: #

slide-4
SLIDE 4
  • &#
  • fdepthh=Perfecttreesofdepth(h)1)

+

  • neormoreleavesatlevelh.

Leavesgoattheleft

  • Notationforbinarytree.

Inthebook: childrenare“completed” with“fake” nodes Thegreensquarednodesarethedummynodes. InthiswayALLtheoriginalnodesareinternal. Theleavesarethefakegreennodes. AlltreesareFULL

  • #'

rightchild leftchild

  • DecisionTree
  • Binarytreeassociatedwithadecisionprocess

– internalnodes:questionswithyes/noanswer – externalnodes:decisions

  • Example:diningdecision

)A =+A ">A + B

  • C

D # D # D #

#

  • ArithmeticExpressionTree

%

– internalnodes:operators – externalnodes:operands

#

+ × × − %

  • 1

5 + Example:arithmeticexpression treefortheexpression (2× (a) 1)+(3× b))

  • #
  • Notation
  • #ofnodes

#ofleaves

  • #ofinternalnodes

height Maximumnumberof nodesateachlevel?

Level0 Level1 1 2 4 8 Level2 leveli))))))) 2i

slide-5
SLIDE 5
  • #
  • Notation

numberofnodes numberofleaves

  • numberof

internalnodes height

  • Properties:

– ( ' 1 – ( 2! 1 – ≤ ≤ ≤ ≤ – ≤ ≤ ≤ ≤ (! 1))2 – ≤ ≤ ≤ ≤ 2 – ≥ ≥ ≥ ≥ log2 – ≥ ≥ ≥ ≥ log2 (' 1) ! 1

  • ( ' "
  • ( ' "
  • ( ' "
  • ( 2! 1

( + ( *' ( ' 1(justproved) (!+

≤ ≤ ≤ (h=maxn.ofancestors) (,- (. Ex:h=3,i=3

Theremustbeatleastoneinternalnodeforeachlevel (exceptthelast)!

slide-6
SLIDE 6

≤ ≤ ≤ #

leveli))))))) maxn.ofnodesis2i h=3 23 leaves ifallatlast levelh

  • therwiseless
  • /≤

≤ ≤ ≤ #

≥ ≥ ≥ ≥ log2 log2 ≤

≤ ≤ ≤ log2 #

log2 ≤

≤ ≤ ≤ h

  • &'

withheighththereare 2h+1 )1nodes Ateachlevelthereare2l nodes,sothetreehas:

h

∑ 2l =1+2+4+… +2h =2h+1)1

  • ( 2h+1 )1
  • l=0
  • InBinarytrees:

log(n+1)≤

≤ ≤ ≤ h+1

h≥

≥ ≥ ≥ log(n+1))1

Asaconsequence: n≤

≤ ≤ ≤ 2h+1)1

n+1≤

≤ ≤ ≤ 2h+1

  • bviously ≤

≤ ≤ ≤ 2h+1 )1

  • 0&#1

with heighth ≤ " ≤ #$ Frompreviousobservation:n≤ ≤ ≤ ≤ 2h+1) 1 n≥

≥ ≥ ≥ 2h

  • Acompletebinarytreeisaperfectbinarytreeofheight

h)1plussomemoreleaves… 2h) 1

  • Height ofa with

nnodes:  %" 02 n≥

≥ ≥ ≥ 2h

slide-7
SLIDE 7
  • #
  • accessor methods

)leftChild(p),rightChild(p),sibling(p)

  • updatemethods

)expandExternal(p),removeAboveExternal(p)

  • therapplicationspecificmethods
  • Pre),post),in) (order)
  • Refertotheplaceoftheparent

relativetothechildren

  • pre isbefore:parent,child,child
  • post isafter:child,child,parent
  • in

isinbetween:child,parent,child

#

  • #

Preorder,Postorder,

3(T,) ()

  • 3 (-45& ")

3 (-46 & ") 3(T,)

  • 3 (-45& ")

3(-46 & ") ()

  • Inorder

!"

3(T,)

  • 3 (-45& ")

()

  • 3(-46 & ")

#

  • +

ArithmeticExpressions

Inorder:a– b Postorder:ab– Preorder– ab + × × − %

  • 1

5 + %× − 1+ 5× + Inorder: %1E × 3 +× + Postorder:

  • &#'( ) *

+,-$,.-,/ #&*) ( '

  • +01$,.-,/

&'( ) *# 23$,.-,/ &#'( ) *

slide-8
SLIDE 8
  • EvaluateArithmeticExpressions
  • Specializationofa

traversal

– recursivemethod returningthevalueofa subtree – whenvisitinganinternal node,combinethevalues

  • fthesubtrees

← ◊ ◊ ◊ ◊ ← && ◊ ◊ ◊ ◊

+ × × − % 6 1 5 %

  • +

× × − % 6 1 5 %

+

Eval × − % 6 1 × 5 % Eval − 1 6 % Eval Eval × 5 % Eval Eval × 6 1 −

  • PrintArithmeticExpressions
  • Specializationofan

traversal

– printoperandor

  • peratorwhenvisiting

node – print“(“ before traversingleftsubtree – print“)“ aftertraversing rightsubtree

FGBB

  • FHBB

+ × × − %

  • 1

5 + GG%× − 1HH+ G5× +HH %× − 1+ 5× +

  • AlgorithmpreOrderTraversalwithStack(T)

StackS TreeNodeN S.push(T)//pushthereferencetoTintheemptystack While(notS.empty()) N=S.pop() if(N!=null){ print(N.elem)//printinformation S.push(N.rightChild)//pushthereferenceto theleftchild S.push(N.leftChild)//pushthereferenceto therightchild }

  • Algorithm preOrderTraversalwithStack(T)
  • +
  • &
  • S.push(T)//pushthereferencetoTintheemptystack

N=S.pop() print(N.elem)

  • Algorithm preOrderTraversalwithStack(T)
  • +
  • &
  • S.push(T)//pushthereferencetoTintheemptystack

N=S.pop() print(N.elem) N

slide-9
SLIDE 9
  • Algorithm preOrderTraversalwithStack(T)
  • +
  • &
  • S.push(N.rightChild)//pushthereferenceto

theleftchild

  • S.push(N.leftChild)//pushthereferenceto

therightchild

  • Algorithm preOrderTraversalwithStack(T)
  • +
  • &
  • N=S.pop()
  • Algorithm preOrderTraversalwithStack(T)
  • +
  • &
  • N=S.pop()
  • N

print(N.elem)

+

  • Algorithm preOrderTraversalwithStack(T)
  • +
  • &
  • +
  • S.push(N.rightChild)

S.push(N.leftChild)

  • generictraversalofabinarytree
  • thepreorder,inorder,andpostorder traversalsare

specialcasesoftheEuler tourtraversal

  • “walkaround” thetreeandvisiteachnodethreetimes:

– ontheleft – frombelow – ontheright

  • (T,)
  • "
  • (-45& ")

2"

  • (-46 & ")

"

slide-10
SLIDE 10
  • 0#14
  • 0#

2 57/

  • Anodeisrepresented

byanobjectstoring

– Element – Parentnode – Leftchildnode – Rightchildnode

  • Nodeobjects

implementthePosition ADT

(

∅ ∅ ∅ ∅ ∅ ∅ ∅ ∅ ∅ ∅ ∅ ∅ ∅ ∅ ∅ ∅ ∅ ∅ ∅ ∅ ∅ ∅ ∅ (

∅ ∅ ∅

  • swapElements(p,q)Input:2PositionsOutput:None

replaceElement(p,e)

Input:PositionandanobjectOutput:Object

isRoot(p)Input:Position

Output:Boolean

isInternal(p)Input:PositionOutput:Boolean isExternal(p)Input:PositionOutput:Boolean leftChild(p),rightChild(p),sibling(p):

Input:PositionOutput:Position

  • #8

39 Element #8 left,right,parent

  • leftChild(v)returnv.left

rightChild(v)returnv.right sibling(v) p← parent(v) q← leftChild(p) if(v=q)returnrightChild(p) elsereturnq

  • replaceElement(v,obj)

temp← v.element v.element← obj returntemp swapElements(v,w) temp← w.element w.element← v.element v.element← temp

  • &-"454

&-"4 ,42""&4

  • 6"&

74%74'"%/

slide-11
SLIDE 11
  • expandExternal(v):

new1andnew2arethenewnodes ifisExternal(v) v.left← new1 v.right← new2 size← size+2

Otherinterestingmethodsforthe ADTBinaryTree:

expandExternal(v):Transformvfromanexternal nodeintoaninternalnodebycreatingtwonewchildren

(

  • (
  • =1

=%

  • removeAboveExternal(v):

(

  • ,

(

  • ,

(

  • ,
  • removeAboveExternal(v):

ifisExternal(v) {p← parent(v) s← sibling(v) ifisRoot(v)s.parent← nullandroot← s else {g← parent(p) ifpisleftChild(g)g.left← s elseg.right← s s.parent← g } size← size)2}

(

  • ,

(

  • ,

(

  • ,
  • p

g s v p s

  • 0& #

2 :!"

7

  • 8
  • 9
  • :
  • 3
  • ;
  • <
  • =

2 . :

  • !

" # $ % & ' ( ) * +

  • ,
  • <&>1?@

1, +&"1?@ ,% 1?@ <1?@

&-"454 &-"4 ,42""&4

  • 6"&
  • "
  • 74%74'"%/

"A

  • 1?@
  • ≤ "

1?#@

  • #≤ "

1?@

  • B

1?@

  • 1≠

1,C-

  • B"
  • &-"454

&-"4 ,42""&4

  • 6"&

74%74'"%/

slide-12
SLIDE 12

2 57/

  • Anodeisrepresented

byanobjectstoring

– Element – Parentnode – Sequenceofchildren nodes

  • Nodeobjectsimplement

thePositionADT

(

  • (

∅ ∅

  • 6

treeT binarytreeT'representingT

  • uinT

u’ inT’ firstchildofuinTisleftchildofu’ inT’ firstsiblingofuinTisrightchildofu’ inT’ RULES

  • #

&

  • 5

B A D I C E G F H L

  • RULE:

touinTcorrespondsu’ inT’ IfuisinternalinTandvisitsfirstchild thenv’ istheleftchildofu’ inT’ ifuisaleafinTandhasnosiblings, thenthechildrenofu’ areleaves Ifvhasasiblingwimmediatelyfollowingit, w’ istherightchildofv’ inT’

5

L

&

  • D

C D C E