added weight method #1

wants to merge 2 commits into

2 participants


I added a defmethod that specialized on weighted-edge-mixin. It's basically a one-liner, but it's a one-liner that anyone using cl-graph for weighted graphs will need at some point.


Hi there, I think the current code is correct. A vertex is a root if nothing points at. Your change would say that a vertex is a root if it doesn't point at anything. Do you agree?

Consider the following example:

(setf g (make-container 'graph-container))
(mapc (lambda (x) (add-vertex g x)) '(1 2 3 4))
(mapc (lambda (x y ) (add-edge-between-vertexes g x y :edge-type :directed)) '(1 4 4) '(4 2 3))
(topological-sort g)

I believe this the last call should return either (#1 #4 #2 #3) or (#1 #4 #3 #2), but in the original code it returns (#1 #2 #3 #4). With my change, it returns the correct order.


The weight method isn't necessary between the
weighted-edge-mixin already defines an accessor (using the ia
notation of defclass*).

So even without your change, we can:

metabang.graph(3): (setf g (make-container 'graph-container))
#<graph-container [0,0] @ #x100124a402>
metabang.graph(4): (setf w (add-edge-between-vertexes g 'a 'b :edge-class 'weighted-edge))
#<weighted-edge <#<a> #<b> nil>>
metabang.graph(10): (weight w)
metabang.graph(9): (setf (weight w) 34)
metabang.graph(10): (weight w)


That's fair. I think it just deviated from the behavior I expected.

(setf g (make-container 'graph-container :edge-class 'weighted-edge))
(add-edge-between-vertexes g 'a 'b :weight 4)
(add-edge-between-vertexes g 'b 'c :weight 5)

Is it possible to do something like the above, where you can just specify the edge-class once rather than in every call to add-edge...?


Ah, I see. My apologies for the lousy documentation (and the lack of error checking for an unsupported initarg). You can do what you want with :default-edge-class.

> (setf g (make-container 'graph-container :default-edge-class 'weighted-edge))
#<graph-container [0,0] @ #x1001a4ef92>
> (add-edge-between-vertexes g 'a 'b :weight 4)
#<weighted-edge <#<a> #<b> nil>>


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment