j-o-caml (2) [email protected] pauillac.inria.fr/~levy/qinghua/j-o-caml qinghua, november...
TRANSCRIPT
![Page 2: J-O-Caml (2) jean-jacques.levy@inria.fr pauillac.inria.fr/~levy/qinghua/j-o-caml Qinghua, November 20](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f145503460f94c28a73/html5/thumbnails/2.jpg)
Plan of this class
• compiling programs
• list processing
• pattern-matching
• new data types
• labeling algorithm
![Page 3: J-O-Caml (2) jean-jacques.levy@inria.fr pauillac.inria.fr/~levy/qinghua/j-o-caml Qinghua, November 20](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f145503460f94c28a73/html5/thumbnails/3.jpg)
Compiling programs
prog1.ml
program textocamlc
binary
a.out
• ocamlc -o prog1 produces prog1 (executable byte code)
• ocamlc -c prog1.ml produces prog1.cmo (byte code)
• ocamlopt -c prog1.ml produces prog1.cmx (binary)
• also files prog1.cmi (compiled interfaces -- see later)
![Page 4: J-O-Caml (2) jean-jacques.levy@inria.fr pauillac.inria.fr/~levy/qinghua/j-o-caml Qinghua, November 20](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f145503460f94c28a73/html5/thumbnails/4.jpg)
Printing
• or if printing many times:
![Page 5: J-O-Caml (2) jean-jacques.levy@inria.fr pauillac.inria.fr/~levy/qinghua/j-o-caml Qinghua, November 20](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f145503460f94c28a73/html5/thumbnails/5.jpg)
List processing
![Page 6: J-O-Caml (2) jean-jacques.levy@inria.fr pauillac.inria.fr/~levy/qinghua/j-o-caml Qinghua, November 20](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f145503460f94c28a73/html5/thumbnails/6.jpg)
List processing
• polymorphic types
• ‘a list (say ``alpha list’’) is type of list of any type, e.g. int list, bool list, ...
![Page 7: J-O-Caml (2) jean-jacques.levy@inria.fr pauillac.inria.fr/~levy/qinghua/j-o-caml Qinghua, November 20](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f145503460f94c28a73/html5/thumbnails/7.jpg)
List processing
• Iterators on lists (~ arrays), but no List.make
• List.map f x
• List.iter f x
• List.fold_left f a x
• List.fold_right f a x
![Page 8: J-O-Caml (2) jean-jacques.levy@inria.fr pauillac.inria.fr/~levy/qinghua/j-o-caml Qinghua, November 20](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f145503460f94c28a73/html5/thumbnails/8.jpg)
Exercices on lists
• Write mem a x which tests if a belongs to list x
• Write exists p x which tests if predicate p is true for one element of x; same forall p x which tests for all elements.
• Arbitrary precision numbers can be implemented by lists (little endian or big endian style). Write addition and multiplication algorithms.
• Conway sequence of lists starts with list [1]. Then next list is read from the previous one. Therefore [1; 1], [2; 1], [1; 2; 1; 1], [1; 1; 1; 2; 2; 1].... Print lists of Conway sequence. Is there any unlucky number ?
![Page 9: J-O-Caml (2) jean-jacques.levy@inria.fr pauillac.inria.fr/~levy/qinghua/j-o-caml Qinghua, November 20](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f145503460f94c28a73/html5/thumbnails/9.jpg)
New data types
• products
![Page 10: J-O-Caml (2) jean-jacques.levy@inria.fr pauillac.inria.fr/~levy/qinghua/j-o-caml Qinghua, November 20](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f145503460f94c28a73/html5/thumbnails/10.jpg)
New data types
• products
![Page 11: J-O-Caml (2) jean-jacques.levy@inria.fr pauillac.inria.fr/~levy/qinghua/j-o-caml Qinghua, November 20](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f145503460f94c28a73/html5/thumbnails/11.jpg)
New data types
• sums
![Page 12: J-O-Caml (2) jean-jacques.levy@inria.fr pauillac.inria.fr/~levy/qinghua/j-o-caml Qinghua, November 20](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f145503460f94c28a73/html5/thumbnails/12.jpg)
New data types
• sums
![Page 13: J-O-Caml (2) jean-jacques.levy@inria.fr pauillac.inria.fr/~levy/qinghua/j-o-caml Qinghua, November 20](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f145503460f94c28a73/html5/thumbnails/13.jpg)
New data types
• recursive types
![Page 14: J-O-Caml (2) jean-jacques.levy@inria.fr pauillac.inria.fr/~levy/qinghua/j-o-caml Qinghua, November 20](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f145503460f94c28a73/html5/thumbnails/14.jpg)
New data types
• recursive types
![Page 15: J-O-Caml (2) jean-jacques.levy@inria.fr pauillac.inria.fr/~levy/qinghua/j-o-caml Qinghua, November 20](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f145503460f94c28a73/html5/thumbnails/15.jpg)
New data types
• recursive types
• recursive polymorphic types
![Page 16: J-O-Caml (2) jean-jacques.levy@inria.fr pauillac.inria.fr/~levy/qinghua/j-o-caml Qinghua, November 20](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f145503460f94c28a73/html5/thumbnails/16.jpg)
New data types
• recursive types
• recursive polymorphic types
![Page 17: J-O-Caml (2) jean-jacques.levy@inria.fr pauillac.inria.fr/~levy/qinghua/j-o-caml Qinghua, November 20](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f145503460f94c28a73/html5/thumbnails/17.jpg)
New data types
• recursive polymorphic types (alternative definition for binary trees)
![Page 18: J-O-Caml (2) jean-jacques.levy@inria.fr pauillac.inria.fr/~levy/qinghua/j-o-caml Qinghua, November 20](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f145503460f94c28a73/html5/thumbnails/18.jpg)
New data types
• recursive polymorphic types (alternative definition for binary trees)
![Page 19: J-O-Caml (2) jean-jacques.levy@inria.fr pauillac.inria.fr/~levy/qinghua/j-o-caml Qinghua, November 20](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f145503460f94c28a73/html5/thumbnails/19.jpg)
Caring about space
• with an extra argument as an accumulator of the result
![Page 20: J-O-Caml (2) jean-jacques.levy@inria.fr pauillac.inria.fr/~levy/qinghua/j-o-caml Qinghua, November 20](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f145503460f94c28a73/html5/thumbnails/20.jpg)
Combien d’objetsdans une image?
Jean-Jacques LévyINRIA
![Page 21: J-O-Caml (2) jean-jacques.levy@inria.fr pauillac.inria.fr/~levy/qinghua/j-o-caml Qinghua, November 20](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f145503460f94c28a73/html5/thumbnails/21.jpg)
Labeling
16 objects in this picture
1
1
2 3
4 5 6
78 9
10
11 12
13 14
![Page 22: J-O-Caml (2) jean-jacques.levy@inria.fr pauillac.inria.fr/~levy/qinghua/j-o-caml Qinghua, November 20](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f145503460f94c28a73/html5/thumbnails/22.jpg)
Naive algorithm
1) choose an unvisited pixel
2) traverse all similar connected pixels
3) and restart until all pixel are visited
Very high complexity:
- how to find an unvisited pixel ?
- how organizing exploration of connected pixels (which direction?)
![Page 23: J-O-Caml (2) jean-jacques.levy@inria.fr pauillac.inria.fr/~levy/qinghua/j-o-caml Qinghua, November 20](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f145503460f94c28a73/html5/thumbnails/23.jpg)
Algorithm
1) first pass
2) scan pixels left-to-right, top-to-bottom giving a new object id each time a new object is met
3) second pass
4) generate equivalences between ids due to new adjacent relations met during scan of pixels.
5) third pass
- compute the number of equivalence classes
Complexity:
- scan twice full image (linear cost)
- try to efficiently manage equivalence classes (Union-Find by Tarjan)
![Page 24: J-O-Caml (2) jean-jacques.levy@inria.fr pauillac.inria.fr/~levy/qinghua/j-o-caml Qinghua, November 20](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f145503460f94c28a73/html5/thumbnails/24.jpg)
Animation
with Polka system for animated algorithms
![Page 25: J-O-Caml (2) jean-jacques.levy@inria.fr pauillac.inria.fr/~levy/qinghua/j-o-caml Qinghua, November 20](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f145503460f94c28a73/html5/thumbnails/25.jpg)
Exercise for next class
• find an algorithm for the labeling algorithm
![Page 26: J-O-Caml (2) jean-jacques.levy@inria.fr pauillac.inria.fr/~levy/qinghua/j-o-caml Qinghua, November 20](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f145503460f94c28a73/html5/thumbnails/26.jpg)
Equivalence classes
![Page 27: J-O-Caml (2) jean-jacques.levy@inria.fr pauillac.inria.fr/~levy/qinghua/j-o-caml Qinghua, November 20](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f145503460f94c28a73/html5/thumbnails/27.jpg)
Equivalence classes“Union-Find”
• objects
• equivalences
• find the equivalence class of
• 3 operations:
-
-
-
new object
find canonical representative
merge 2 equivalence classes
![Page 28: J-O-Caml (2) jean-jacques.levy@inria.fr pauillac.inria.fr/~levy/qinghua/j-o-caml Qinghua, November 20](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f145503460f94c28a73/html5/thumbnails/28.jpg)
Equivalence classes“Union-Find”
• with tree-like structure
• given by the array of direct ancestors
ancestor
![Page 29: J-O-Caml (2) jean-jacques.levy@inria.fr pauillac.inria.fr/~levy/qinghua/j-o-caml Qinghua, November 20](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f145503460f94c28a73/html5/thumbnails/29.jpg)
Equivalence classes“Union-Find”
• unbalanced trees because of merges
- merge 2 equivalence classes
![Page 30: J-O-Caml (2) jean-jacques.levy@inria.fr pauillac.inria.fr/~levy/qinghua/j-o-caml Qinghua, November 20](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f145503460f94c28a73/html5/thumbnails/30.jpg)
Equivalence classes“Union-Find”
• try to balance
- merge 2 equivalence classes
therefore keep height at each node
opérations pour
![Page 31: J-O-Caml (2) jean-jacques.levy@inria.fr pauillac.inria.fr/~levy/qinghua/j-o-caml Qinghua, November 20](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f145503460f94c28a73/html5/thumbnails/31.jpg)
Equivalence classes“Union-Find”
• compression of path toward canonical representative
- with side-effect on tree structure
![Page 32: J-O-Caml (2) jean-jacques.levy@inria.fr pauillac.inria.fr/~levy/qinghua/j-o-caml Qinghua, November 20](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f145503460f94c28a73/html5/thumbnails/32.jpg)
Exercice for next class
• find good primitives for bitmap graphics in Ocaml library
• design the overall structure of the labeling program