HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Actionscript Graph data structure implementation

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
implementationgraphstructureactionscriptdata

Problem

So, I am not sure if I should have Vertex extend Point, because the equals() and compareTo() functions are already parameterized with Point. I guess I don't really need compare to...

Does this structure look good? I used a Map instead of a TreeMap because as3commons and the default flex packages do not have this structure defined. How easy will this be if I were to create it.

Things that I am in the process of working out now:

  • I know I need to add removeVertex() (and removeEdge()?)



-
If I want to create an Edge class, I supposed I would make the following changes:

// Since this is undirected, do I need 2 edges, because I have src and dest?
public class Edge {
    public var src:Vertex;
    public var dest:Vertex;
    public var data:Object;

    public function Edge(src:Vertex, dest:Vertex, data:Object=null) {
        // Set instance variables
    }
}

public class Graph {
    public var edges:IMap; // 

    public function addEdge(from:Object, to:Object, data:Object):void {
        edges.add(data, new Edge(v, w));
    }
}


My main gripe would be about juggling all of these data types.

  • public var adjList:IMap; //



  • public var vertices:IMap; //



  • public var edges:IMap; //



This may make if difficult to intelligently traverse the graph.

Reference classes and files:

Vertex.as

```
package core {
import flash.geom.Point;

public class Vertex extends Point {
public var data:Object;

public function Vertex(data:Object=null, x:Number=0, y:Number=0) {
super(x, y);

this.data = data;
}

// Implement...
override public function clone():Point {
return new Vertex(data, x, y);
}

override public function toString():String {
return data.toString();
}

// Implement...
override public function equals(toCompare:Point):Boolean {
return super.equals(toCompare) && this.compareTo(toCompare

Solution

adjList needs a more descriptive name. Specifically as a class member variable, it deserves the few extra characters it would take to be called adjacencyList.

Additionally, you have extended Point, but do not use any of Point's functions. Does Point even provide functions? (Yes, it does.)

I'd have gone with a Vertex class that contains a Point, not extends it. You pointed this out yourself in a comment as well.

public function adjacentTo(value:*):ISet {
        if (value is Object && hasVertex(value as Object)) {
            return adjList.itemFor(getVertex(value as Object)) as Set;
        }

        if (value is Vertex && adjList.has(value as Vertex)) {
            return adjList.itemFor(value as Vertex) as Set;
        }

        return EMPTY_SET;
    }


This is a nasty bit of code. I wanted to say that you should swap the two checks, as any Vertex is also an Object, and you end up always checking if any of your vertices has value as data. But if you were to have a Vertex which has another Vertex as data then swapping the two would make the function fail to perform properly. A comment along these lines would go a long way towards not shooting yourself in the foot later.

public function addVertex(data:Object):Vertex {
        var v:Vertex = vertices.itemFor(data);
        //codes
    }

    public function getVertex(data:Object):Vertex {
        return vertices.itemFor(data);
    }


Reuse your functions where you can; like that you allow code to be read at a semantic level, rather than a syntactical level. Logical reasoning likes to work with semantics and not bother so much with the syntax. In this case, you can reuse getVertex:

public function addVertex(data:Object):Vertex {
        var v:Vertex = getVertex(data);
        //codes
    }

Code Snippets

public function adjacentTo(value:*):ISet {
        if (value is Object && hasVertex(value as Object)) {
            return adjList.itemFor(getVertex(value as Object)) as Set;
        }

        if (value is Vertex && adjList.has(value as Vertex)) {
            return adjList.itemFor(value as Vertex) as Set;
        }

        return EMPTY_SET;
    }
public function addVertex(data:Object):Vertex {
        var v:Vertex = vertices.itemFor(data);
        //codes
    }

    public function getVertex(data:Object):Vertex {
        return vertices.itemFor(data);
    }
public function addVertex(data:Object):Vertex {
        var v:Vertex = getVertex(data);
        //codes
    }

Context

StackExchange Code Review Q#42262, answer score: 2

Revisions (0)

No revisions yet.