Docsity

List Processing Algorithms: Representing Trees and Membership in Lists - Prof. Hanan Samet, Study notes of Data Structures and Algorithms

Toggle sidebar
bg1
lp1
8
1
b
INTRODUCTION
• The primary data structure is a list, e.g.,
(A B C D E)
(A)
() empty list or NIL = a special name
• Can represent any entities
x + y
first item is operator, remaining items are operands
can have an arbitrary number of arguments
xy + x + 3
• We can refer to elements of a list by using brackets:
for L = (PLUS (TIMES x y) x 3) we have
L[1] =
L[2] =
L[2,2] =
L[4] =
(∃x)(∀y) P(x) ⊃ P(y)
• An undirected graph
• Questions:
1. How would we represent it?
2. What do we want to know?
3. What node is connected to what node?
• Solution: list of lists where first element of each
list is connected to rest
AB
C
D
EF
Copyright © 2003 by Hanan Samet
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

Download List Processing Algorithms: Representing Trees and Membership in Lists - Prof. Hanan Samet and more Study notes Data Structures and Algorithms in PDF only on Docsity!

(^81) lp b INTRODUCTION

  • The primary data structure is a list, e.g., (A B C D E) (A) () empty list or NIL = a special name
  • Can represent any entities x + y first item is operator, remaining items are operands can have an arbitrary number of arguments xy + x + 3
  • We can refer to elements of a list by using brackets: for L = (PLUS (TIMES x y) x 3) we have L[1] = L[2] = L[2,2] = L[4] = (∃x)(∀y) P(x) ⊃ P(y)
  • An undirected graph
  • Questions:
    1. How would we represent it?
    2. What do we want to know?
    3. What node is connected to what node?
  • Solution: list of lists where first element of each list is connected to rest

A B

C

D

E F

Copyright © 2003 by Hanan Samet

(^81) lp b INTRODUCTION

  • The primary data structure is a list, e.g., (A B C D E) (A) () empty list or NIL = a special name
  • Can represent any entities x + y first item is operator, remaining items are operands can have an arbitrary number of arguments xy + x + 3
  • We can refer to elements of a list by using brackets: for L = (PLUS (TIMES x y) x 3) we have L[1] = L[2] = L[2,2] = L[4] = (∃x)(∀y) P(x) ⊃ P(y)
  • An undirected graph
  • Questions:
    1. How would we represent it?
    2. What do we want to know?
    3. What node is connected to what node?
  • Solution: list of lists where first element of each list is connected to rest

A B

C

D

E F

Copyright © 2003 by Hanan Samet

(^2) lp r

(PLUS x y)

Copyright © 2003 by Hanan Samet

(^81) lp b INTRODUCTION

  • The primary data structure is a list, e.g., (A B C D E) (A) () empty list or NIL = a special name
  • Can represent any entities x + y first item is operator, remaining items are operands can have an arbitrary number of arguments xy + x + 3
  • We can refer to elements of a list by using brackets: for L = (PLUS (TIMES x y) x 3) we have L[1] = L[2] = L[2,2] = L[4] = (∃x)(∀y) P(x) ⊃ P(y)
  • An undirected graph
  • Questions:
    1. How would we represent it?
    2. What do we want to know?
    3. What node is connected to what node?
  • Solution: list of lists where first element of each list is connected to rest

A B

C

D

E F

Copyright © 2003 by Hanan Samet

(^2) lp r

(PLUS x y)

Copyright © 2003 by Hanan Samet

(^3) lp z

(PLUS (TIMES x y) x 3)

Copyright © 2003 by Hanan Samet

(^4) lp g

PLUS

Copyright © 2003 by Hanan Samet

(^81) lp b INTRODUCTION

  • The primary data structure is a list, e.g., (A B C D E) (A) () empty list or NIL = a special name
  • Can represent any entities x + y first item is operator, remaining items are operands can have an arbitrary number of arguments xy + x + 3
  • We can refer to elements of a list by using brackets: for L = (PLUS (TIMES x y) x 3) we have L[1] = L[2] = L[2,2] = L[4] = (∃x)(∀y) P(x) ⊃ P(y)
  • An undirected graph
  • Questions:
    1. How would we represent it?
    2. What do we want to know?
    3. What node is connected to what node?
  • Solution: list of lists where first element of each list is connected to rest

A B

C

D

E F

Copyright © 2003 by Hanan Samet

(^2) lp r

(PLUS x y)

Copyright © 2003 by Hanan Samet

(^3) lp z

(PLUS (TIMES x y) x 3)

Copyright © 2003 by Hanan Samet

(^4) lp g

PLUS

Copyright © 2003 by Hanan Samet

(^5) lp v

(TIMES x y)

Copyright © 2003 by Hanan Samet

(^81) lp b INTRODUCTION

  • The primary data structure is a list, e.g., (A B C D E) (A) () empty list or NIL = a special name
  • Can represent any entities x + y first item is operator, remaining items are operands can have an arbitrary number of arguments xy + x + 3
  • We can refer to elements of a list by using brackets: for L = (PLUS (TIMES x y) x 3) we have L[1] = L[2] = L[2,2] = L[4] = (∃x)(∀y) P(x) ⊃ P(y)
  • An undirected graph
  • Questions:
    1. How would we represent it?
    2. What do we want to know?
    3. What node is connected to what node?
  • Solution: list of lists where first element of each list is connected to rest

A B

C

D

E F

Copyright © 2003 by Hanan Samet

(^2) lp r

(PLUS x y)

Copyright © 2003 by Hanan Samet

(^3) lp z

(PLUS (TIMES x y) x 3)

Copyright © 2003 by Hanan Samet

(^4) lp g

PLUS

Copyright © 2003 by Hanan Samet

(^5) lp v

(TIMES x y)

Copyright © 2003 by Hanan Samet

(^6) lp r

x

Copyright © 2003 by Hanan Samet

(^7) lp z

Copyright © 2003 by Hanan Samet

(^81) lp b INTRODUCTION

  • The primary data structure is a list, e.g., (A B C D E) (A) () empty list or NIL = a special name
  • Can represent any entities x + y first item is operator, remaining items are operands can have an arbitrary number of arguments xy + x + 3
  • We can refer to elements of a list by using brackets: for L = (PLUS (TIMES x y) x 3) we have L[1] = L[2] = L[2,2] = L[4] = (∃x)(∀y) P(x) ⊃ P(y)
  • An undirected graph
  • Questions:
    1. How would we represent it?
    2. What do we want to know?
    3. What node is connected to what node?
  • Solution: list of lists where first element of each list is connected to rest

A B

C

D

E F

Copyright © 2003 by Hanan Samet

(^2) lp r

(PLUS x y)

Copyright © 2003 by Hanan Samet

(^3) lp z

(PLUS (TIMES x y) x 3)

Copyright © 2003 by Hanan Samet

(^4) lp g

PLUS

Copyright © 2003 by Hanan Samet

(^5) lp v

(TIMES x y)

Copyright © 2003 by Hanan Samet

(^6) lp r

x

Copyright © 2003 by Hanan Samet

(^7) lp z

Copyright © 2003 by Hanan Samet

(^8) lp g

(EXIST x (ALL y (IMPLIES (P x)(P y))))

Copyright © 2003 by Hanan Samet

(^81) lp b REPRESENTATION OF A LIST

  • Components of lists can beatoms
    1. any sequence of characters not including spaces or parentheses
    2. examples: x y 345 A37 A-B-C 376-80-5763 80.8 …
  • How would we represent a list?
  • In earlier work we used:

where Ω = NIL or

(A B C) would be:

  • What about xy+x+3 or (PLUS (TIMES x y) x 3)?
  • Solution: INFO points to another list!

INFO INFO

LINK last element

Copyright © 2003 by Hanan Samet

(^81) lp b REPRESENTATION OF A LIST

  • Components of lists can beatoms
    1. any sequence of characters not including spaces or parentheses
    2. examples: x y 345 A37 A-B-C 376-80-5763 80.8 …
  • How would we represent a list?
  • In earlier work we used:

where Ω = NIL or

(A B C) would be:

  • What about xy+x+3 or (PLUS (TIMES x y) x 3)?
  • Solution: INFO points to another list!

INFO INFO

LINK last element

Copyright © 2003 by Hanan Samet

(^2) lp r

A B C

Copyright © 2003 by Hanan Samet

lp OBSERVATIONS ABOUT LISTS

  • There is really no need for INFO field
  • There are two link fields, say LLINK and RLINK
  • INFO is now an atom, which is a link to aproperty list
    1. value of the atom
    2. print name
  • Notation
    1. use lower-case letters at the end of the alphabet (e.g., x, y, z) to describe variables and upper-case letters at the start of the alphabet (e.g., A, B, C, D) to denote data
    2. atom represented by address of its property list
    3. list referred to by address of its first element
  • Note a curious asymmetry:
    1. LLINK can refer to atom or list, but
    2. RLINK can only refer to a list or the empty list (equivalent to the atom NIL)

Copyright © 2003 by Hanan Samet

(^81) lp b S-EXPRESSIONS

  • An atom or a pair of s-expressions separated by. and surrounded by parentheses

⇒ | ( . )

  • Examples: A (A.B) (A.(B.A)) (3. 3.4) note convention about decimal point and dot: . is not a decimal point. Space around dot may be omitted if no confusion results: (PLUS.(x.(y.NIL)))
  • Represented in computer memory by:

(A.B) → (A.(B.A)) →

(PLUS.(x.(y.NIL))) →

  • This should be familiar

sexpr sexpr

B A

A

Copyright © 2003 by Hanan Samet

(^81) lp b S-EXPRESSIONS

  • An atom or a pair of s-expressions separated by. and surrounded by parentheses

⇒ | ( . )

  • Examples: A (A.B) (A.(B.A)) (3. 3.4) note convention about decimal point and dot: . is not a decimal point. Space around dot may be omitted if no confusion results: (PLUS.(x.(y.NIL)))
  • Represented in computer memory by:

(A.B) → (A.(B.A)) →

(PLUS.(x.(y.NIL))) →

  • This should be familiar

sexpr sexpr

B A

A

Copyright © 2003 by Hanan Samet

(^2) lp r

A B

Copyright © 2003 by Hanan Samet

(^3) lp z

PLUS x y

Copyright © 2003 by Hanan Samet

(^81) lp b S-EXPRESSIONS

  • An atom or a pair of s-expressions separated by. and surrounded by parentheses

⇒ | ( . )

  • Examples: A (A.B) (A.(B.A)) (3. 3.4) note convention about decimal point and dot: . is not a decimal point. Space around dot may be omitted if no confusion results: (PLUS.(x.(y.NIL)))
  • Represented in computer memory by:

(A.B) → (A.(B.A)) →

(PLUS.(x.(y.NIL))) →

  • This should be familiar

sexpr sexpr

B A

A

Copyright © 2003 by Hanan Samet

(^2) lp r

A B

Copyright © 2003 by Hanan Samet

(^3) lp z

PLUS x y

Copyright © 2003 by Hanan Samet

(^4) lp g

(PLUS x y)

Copyright © 2003 by Hanan Samet

(^81) lp b LISP EXAMPLES

(A.B) (C.D) CONS['(A.B),'(C.D)] =

CAR['((A.B).(C.D))] =

CAR[CAR '((A.B).(C.D))] =

CAAR['((A.B).(C.D))] =

  • note use of CAAR for CAR( CAR (x) )
  • also CADR (x) = CAR ( CDR(x) )
  • CDR is performed first followed by CAR
  • can construct any combination needed

CONS['(A.B),'A] = ((A.B).A)

CONS['A,'(B C D)] = (A B C D)

Important: CAR[CONS['A,'B]] = CDR[CONS['A,'B]] = CONS[CAR['(A.B)],CDR['(A.B)]] =

A B C D

A

A B

A B C D

Copyright © 2003 by Hanan Samet

(^81) lp b LISP EXAMPLES

(A.B) (C.D) CONS['(A.B),'(C.D)] =

CAR['((A.B).(C.D))] =

CAR[CAR '((A.B).(C.D))] =

CAAR['((A.B).(C.D))] =

  • note use of CAAR for CAR( CAR (x) )
  • also CADR (x) = CAR ( CDR(x) )
  • CDR is performed first followed by CAR
  • can construct any combination needed

CONS['(A.B),'A] = ((A.B).A)

CONS['A,'(B C D)] = (A B C D)

Important: CAR[CONS['A,'B]] = CDR[CONS['A,'B]] = CONS[CAR['(A.B)],CDR['(A.B)]] =

A B C D

A

A B

A B C D

Copyright © 2003 by Hanan Samet

(^2) lp r

A B C D

Copyright © 2003 by Hanan Samet