Object Oriented implementation of graph data structures

后端 未结 4 695
旧巷少年郎
旧巷少年郎 2021-02-02 04:08

I have been reading quite a bit graph data structures lately, as I have intentions of writing my own UML tool. As far as I can see, what I want can be modeled as a simple graph

相关标签:
4条回答
  • 2021-02-02 04:12

    If you're using Objective-C I assume you have access to Core Data which would be probably be a great place to start - I understand you're creating your own graph, the strength of Core Data being that it can do a lot of the checking you're talking about for free if you set up your schema properly

    0 讨论(0)
  • 2021-02-02 04:18

    Check out this powerpoint presentation, especially the last slide: http://digital.cs.usu.edu/~cyan/CS5050/Graph.ppt

    0 讨论(0)
  • 2021-02-02 04:29

    Here are the two basic graph types along with their typical implementations:

    Dense Graphs:

    • Adjacency Matrix
    • Incidence Matrix

    Sparse Graphs:

    • Adjacency List
    • Incidence List

    In the graph framework (closed source, unfortunately) that I've ben writing (>12k loc graph implementations + >5k loc unit tests and still counting) I've been able to implement (Directed/Undirected/Mixed) Hypergraphs, (Directed/Undirected/Mixed) Multigraphs, (Directed/Undirected/Mixed) Ordered Graphs, (Directed/Undirected/Mixed) KPartite Graphs, as well as all kinds of Trees, such as Generic Trees, (A,B)-Trees, KAry-Trees, Full-KAry-Trees, (Trees to come: VP-Trees, KD-Trees, BKTrees, B-Trees, R-Trees, Octrees, …).
    And all without a single vertex or edge class. Purely generics. And with little to no redundant implementations**
    Oh, and as if this wasn't enough they all exist as mutable, immutable, observable (NSNotification), thread-unsafe and thread-safe versions.
    How? Through excessive use of Decorators.
    Basically all graphs are mutable, thread-unsafe and not observable. So I use Decorators to add all kinds of flavors to them (resulting in no more than 35 classes, vs. 500+ if implemented without decorators, right now).

    While I cannot give any actual code, my graphs are basically implemented via Incidence Lists by use of mainly NSMutableDictionaries and NSMutableSets (and NSMutableArrays for my ordered Trees).

    My Undirected Sparse Graph has nothing but these ivars, e.g.:

    NSMutableDictionary *vertices;
    NSMutableDictionary *edges;
    

    The ivar vertices maps vertices to adjacency maps of vertices to incident edges ({"vertex": {"vertex": "edge"}})
    And the ivar edges maps edges to incident vertex pairs ({"edge": {"vertex", "vertex"}}), with Pair being a pair data object holding an edge's head vertex and tail vertex.

    Mixed Sparse Graphs would have a slightly different mapping of adjascency/incidence lists and so would Directed Sparse Graphs, but you should get the idea.

    A limitation of this implementation is, that both, every vertex and every edge needs to have an object associated with it. And to make things a bit more interesting(sic!) each vertex object needs to be unique, and so does each edge object. This is as dictionaries don't allow duplicate keys. Also, objects need to implement NSCopying. NSValueTransformers or value-encapsulation are a way to sidestep these limitation though (same goes for the memory overhead from dictionary key copying).

    While the implementation has its downsides, there's a big benefit: immensive versatility! There's hardly any type graph that I could think of that's impossible to archieve with what I already have. Instead of building each type of graph with custom built parts you basically go to your box of lego bricks and assemble the graphs just the way you need them.

    Some more insight:

    Every major graph type has its own Protocol, here are a few:

    HypergraphProtocol
        MultigraphProtocol [tagging protocol] (allows parallel edges)
        GraphProtocol (allows directed & undirected edges)
            UndirectedGraphProtocol [tagging protocol] (allows only undirected edges)
            DirectedGraphProtocol [tagging protocol] (allows only directed edges)
                ForestProtocol (allows sets of disjunct trees)
                    TreeProtocol (allows trees)
                        ABTreeProtocol (allows trees of a-b children per vertex)
                            FullKAryTreeProtocol [tagging protocol] (allows trees of either 0 or k children per vertex)
    

    The protocol nesting implies inharitance (of both protocols, as well as implementations).

    If there's anything else you'd like to get some mor insight, feel free to leave a comment.

    Ps: To give credit where credit is due: Architecture was highly influenced by the
    JUNG Java graph framework (55k+ loc).

    Pps: Before choosing this type of implementation I had written a small brother of it with just undirected graphs, that I wanted to expand to also support directed graphs. My implementation was pretty similar to the one you are providing in your question. This is what gave my first (rather naïve) project an abrupt end, back then: Subclassing a set of inter-dependent classes in Objective-C and ensuring type-safety Adding a simple directedness to my graph cause my entire code to break apart. (I didn't even use the solution that I posted back then, as it would have just postponed the pain) Now with the generic implementation I have more than 20 graph flavors implemented, with no hacks at all. It's worth it.

    If all you want is drawing a graph and being able to move its nodes on the screen, though, you'd be fine with just implementing a generic graph class that can then later on be extended to specific directedness, if needed.

    0 讨论(0)
  • 2021-02-02 04:34

    An adjacency matrix will have a bit more difficulty than your object model in adding and removing vertices (but not edges), since this involves adding and removing rows and columns from a matrix. There are tricks you could use to do this, like keeping empty rows and columns, but it will still be a bit complicated.

    When moving a vertex around the screen, the edges will also be moved. This also gives your object model a slight advantage, since it will have a list of connected edges and will not have to search through the matrix.

    Both models have an inherent directedness to the edges, so if you want to have undirected edges, then you will have to do additional work either way.

    I would say that overall there is not a whole lot of difference. If I were implementing this, I would probably do something similar to what you are doing.

    0 讨论(0)
提交回复
热议问题