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:
- How would we represent it?
- What do we want to know?
- 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:
- How would we represent it?
- What do we want to know?
- 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:
- How would we represent it?
- What do we want to know?
- 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:
- How would we represent it?
- What do we want to know?
- 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:
- How would we represent it?
- What do we want to know?
- 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:
- How would we represent it?
- What do we want to know?
- 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
- any sequence of characters not including spaces or parentheses
- 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
- any sequence of characters not including spaces or parentheses
- 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
- value of the atom
- print name
- Notation
- 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
- atom represented by address of its property list
- list referred to by address of its first element
- Note a curious asymmetry:
- LLINK can refer to atom or list, but
- 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))) →
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))) →
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))) →
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