Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic graph layout spring theory

I'm trying to position entities visualy to show their relationships to each other. It looks like for automatic graph layout, the spring algorithm would suit my needs. I'd like to implement this in silverlight using c#, so I'm looking for code samples, or links to good explanations of the theory. Any help appreciated

like image 688
Jeremy Avatar asked Aug 25 '09 04:08

Jeremy


People also ask

What is Kamada Kawai layout?

The Kamada-Kawai graph layout attempts to position nodes on the space so that the geometric (Euclidean) distance between them is as close as possible to the graph-theoretic (path) distance between them.

What is Yifan Hu algorithm?

The Yifan Hu Multilevel layout algorithm is an algorithm that brings together the good parts of force-directed algorithms and a multilevel algorithm to reduce algorithm complexity. This is one of the algorithms that works really well with large networks.

What is fruchterman Reingold algorithm?

The Fruchterman-Reingold layout is a force-directed layout algorithm which treats edges like springs that move vertexes closer or further from each other in an attempt to find an equilibrium that minimizes the energy of the system.

What is graph layout algorithm?

A layout algorithm addresses one or more quality criteria, depending on the type of graph and the features of the algorithm, when laying out a graph. The most common criteria are: Minimizing the number of link crossings. Minimizing the total area of the drawing. Minimizing the number of bends (in orthogonal drawings)


2 Answers

I wrote some code awhile ago to perform dynamic graph layouts using C# and XNA (full source available upon request).

Here are some of the critical functions:

        public void UpdateNodes()
        {
            for (int i = 0; i < nodes.Count; i++)
            {
                Vector2 netForce = Vector2.Zero;
                foreach (Node otherNode in nodes)
                {
                    if (otherNode != nodes[i])
                    {
                        netForce += CoulombRepulsion(nodes[i], otherNode); //calculate repulsion for all nodes
                        if (nodes[i].links.Contains(otherNode))
                        {
                            netForce += HookeAttraction(nodes[i], otherNode); //only calc attraction for linked nodes
                        }
                    }
                }
                nodes[i].Velocity += netForce;
                nodes[i].Velocity *= .99f;
                nodes[i].Position += nodes[i].Velocity;
            }
        }


        public Vector2 HookeAttraction(Node node1, Node node2) //ON node1 BY node2
        {
            Vector2 direction = Vector2.Subtract(node2.Position, node1.Position);
            direction.Normalize();

            return hookeConst* node2.Mass * Vector2.Distance(node1.Position, node2.Position) * direction;
        }

        public Vector2 GravAttraction(Node node1, Node node2) //ON node1 BY node2
        {
            Vector2 direction = Vector2.Subtract(node2.Position, node1.Position);
            direction.Normalize();

            return gravConst * node2.Mass * Vector2.DistanceSquared(node1.Position, node2.Position) * direction;
        }

Pick the two constants based on how fast you want the graph to converge. I used these:

        private const float hookeConst = .000005f;
        private const float gravConst = .00000001f;

That code is pretty self-explanatory, but feel free to ask if you need anything. Basically, call the UpdateNodes() function in a loop, and your graph will converge on its minimal-energy state.

like image 180
Preetum Nakkiran Avatar answered Oct 11 '22 18:10

Preetum Nakkiran


I have not used any of these examples but I believe they will be of use to you.

  • Silverlight diagramming with spring embedder layout [demo]
  • Silverlight Bag of tricks
  • A Silverlight graph visualizer (updated)

There is also a similar (duplicate?) question here: Graph visualisation in Silverlight

like image 41
Eric Schoonover Avatar answered Oct 11 '22 20:10

Eric Schoonover