Here's a quickie for you. Given a list of stupendous and garguantuan proportions*, how would you measure or describe its complexity?
flat is useful, I suppose, but I want to know how deep the levels go and that doesn't help. I don't really want to convert it to a string, unless that's the only way...
* - eg some S-XML I found recently... :)
Finding out complexity of a list
-
- Posts: 2038
- Joined: Tue Nov 29, 2005 8:28 pm
- Location: latiitude 50N longitude 3W
- Contact:
Re: Finding out complexity of a list
Maybe ref-all to find reference vectors for all elements, then take the longest vector as the complexity metric? Something like this:
Edit: Clarified list parameter in ref-all.
Code: Select all
(apply max (map length (ref-all nil aList (fn (x) true))))
-
- Posts: 2038
- Joined: Tue Nov 29, 2005 8:28 pm
- Location: latiitude 50N longitude 3W
- Contact:
Re: Finding out complexity of a list
That's cool! Hadn't thought of looking for nil!
-
- Posts: 388
- Joined: Thu May 08, 2008 1:24 am
- Location: Croatia
- Contact:
Re: Finding out complexity of a list
I have these three functions in my library:
(Lists are understood as trees, and complexity is measured in terms of nodes, branches and leafs. The branch is path from root to leaf.)
For example, when my library is loaded, I get messages like this one:
That gives few measures of the complexity of the definition of the random-sublist:
(Lists are understood as trees, and complexity is measured in terms of nodes, branches and leafs. The branch is path from root to leaf.)
Code: Select all
;
; Syntax: (depth <L>) - length of the longest branch
; (size <L>) - number of nodes
; (width <L>) - number of leafs
;
; Examples:
(set 'depth (lambda(x)
;(println x)
(cond ((quote? x)(+ 1 (depth (eval x))))
((and (list? x) (empty? x)) 1)
((list? x)(+ 1 (apply max (map depth x))))
(true 1))))
(set 'size (lambda(x)
(+ 1 (cond ((quote? x)(size (eval x)))
((list? x)(apply + (map size x)))
(true 0)))))
(set 'width (lambda(x)
(cond ((quote? x)(width (eval x)))
((list? x)(apply + (map width x)))
(true 1))))
Code: Select all
Defined random-sublist with size=70, depth=8 and width=43.
Code: Select all
(lambda(L pick-from-list)
(let ((result '())
(left-in-list (length L)))
(when (> pick-from-list left-in-list)
(throw-error (append "There is no n="
(string pick-from-list)
" elements in L.")))
(dolist (element L (= pick-from-list 0))
(let ((probability (div pick-from-list
left-in-list)))
(when (<= (random 0 1) probability)
(push element result -1)
(dec pick-from-list))
(dec left-in-list)))
result))
-
- Posts: 2038
- Joined: Tue Nov 29, 2005 8:28 pm
- Location: latiitude 50N longitude 3W
- Contact:
Re: Finding out complexity of a list
Thanks, Kazimir. I've just spotted your reply! These work well.
where sxml is the newLISP user manual in SXML... :)
Code: Select all
(map (fn (f) (println f { } (apply f sxml))) '(depth size width))
depth 10
size 77477
width 51393