Re: Standard graph API?

From: Magnus Lie Hetland (mlh_at_furu.idi.ntnu.no)
Date: 08/24/04


Date: Tue, 24 Aug 2004 08:40:27 +0000 (UTC)

In article <eppstein-4A56C8.14042623082004@news.service.uci.edu>,
David Eppstein wrote:
[snip]
>I would strongly prefer not to have weights or similar attributes as
>part of a graph API. I would rather have the weights be a separate dict
>or function or whatever passed to the graph algorithm. The main reason
>is that I might want the same algorithm to be applied to the same graph
>with a different set of weights.

I can see that.

[snip stuff about using dicts]

This can be said about all objects, really; no reason to have
attributes as long as we can associate values with the objects using
dicts. This is where things start to look implementation-specific,
even though it is *possible* to keep it abstract using this interface.

One of my motivations is allowing arbitrary structures behind the
scenes, e.g. the graph may be a front-end for something that is
computed on-the-fly using specialized hardware (in fact a very real
possibilit in my case). I could have something like this be
represented by several distinct objects (e.g. one for the topological
structure, one for the weights), of course, but I'd certainly have to
implement the weight mapping myself, and not use built-in dicts.

I do think your API is nice in that it is simple, but I also have the
feeling that using it with other implementations would be sort of
unnatural; one would be trying to emulate the "dict-of-lists with
dicts for properties" implementation, because that implementation
*would* have been simple -- had one used it.

Also, again, this doesn't lend itself very well to manipulating
graphs. If I set one weight to infinity, I might expect (perhaps) the
corresponding edge to disappear (otherwise the graph would have to be
complete in the first place) or similar things; there may also be
other dependencies between properties. Not easily handled with a
separate object for each property. And using functions for everything
that needs calculating doesn't easily lead to polymorphism...

It's not horribly inconvenient, of course (just a matter of defining a
few objects referring to the same underlying mechanism, each with
a different __getitem__ method). I'm just airing my thoughts about why
it *might* be useful to have something else -- possibly in addition.

Perhaps one could have something like two levels? The Level 1 Graph
API would support the "graph as mapping from nodes to neighbors" with
"properties as separate mappings" and the Level 2 Graph API could add
some convenience methods/properties for encapsulation and
manipulation?

[snip]
> I think this may contradict some things I said a year or two ago
> about using a dict-of-dicts representation in which G[v][w] provides
> the weight;

Yeah, I remember you saying that :)

> I've changed my mind.

Fair enough. FWIW, I agree with your new position when it comes to the
simple dict-based implementation; this is basically how it's done in
pseudocode, usually (e.g. having pi[v] represent the predecessor of v
and so forth).

-- 
Magnus Lie Hetland     "Canned Bread: The greatest thing since sliced
http://hetland.org      bread!" [from a can in Spongebob Squarepants]


Relevant Pages

  • Re: Problem on an nxn grid
    ... "See Spot Run" in the vocabulary of Graph Theory. ... Gibbons's "Algorithmic Graph Theory" gives a pseudo-code ... parts of the Edmonds algorithm (such as the Hungarian ... All weights are assumed to be ...
    (sci.math)
  • Re: Shortest Path finding with mutating weights
    ... > Dijkstra's algo can be used to find shortest path between 2 nodes in ... > a weighted graph. ... > How to find shortest paths in a graph with mutating weights? ... Connections from one time slice to the another ...
    (comp.theory)
  • Graph problem, is it NP-Complete?
    ... An undirected weighted graph, ... acyclic and all its vertices have 2 neighbours, ... YES if there exists a set of size <= K of connected subgraphs of the ... The sum of all the weights of each edge of each subgraph that "map" to ...
    (comp.theory)
  • Re: Modeling General Graphs in SQL
    ... You don't mention assigning weights to the edges, so can we assume that the ... Therefore a minimal spanning tree of a graph with n ... Or you might need to test if the graph is cyclic or acyclic. ... return the minimum path between two vertices (wow, ...
    (comp.databases)
  • set, dict and other structures
    ... even if they seem a little slower than dicts. ... I've studied the nice sets python module from Py2.3, ... graph data structure are different from other people needs, ...
    (comp.lang.python)