简体   繁体   中英

How can i define the attributes of edges or vertices by using Jung?

May be it is a naive question but really i couldn't have the function that define the attributes of edges or vertices in graph.

Given that my graph is a DirectedSparseGraph.

Thank you in advance

There are several different ways of working with edge/vertex attributes in JUNG.

(The following material is taken from the JUNG Manual, currently at http://sourceforge.net/apps/trac/jung/wiki/JUNGManual .)

Many of JUNG's methods require the user to specify an association between a graph element (vertex or edge) and data of some sort: label text, edge weight, color, etc. By convention this is generally done via a Transformer.

Transformer is a commons-collections interface with a single method transform(I input) that returns some object of the output type (O) for any input. This essentially defines a relationship between elements of the input and output types. In a sense, it's something like Map, except that it's much more lightweight and is read-only.

There are a few different ways to write one of these Transformers. This applies to any place that you're asked to provide a Transformer.

(1) Constant value. There's actually a ConstantTransformer class (in commons-collections) for this purpose. This is useful for situations in which you're asked for a Transformer but in fact all elements should get the same value (eg providing an edge weight when it's an unweighted graph).

(2) Map-backed--either a new map or based on an existing Map. There's a MapTransformer class (again, commons-collections) that handles this case. This is useful when you have about as many distinct values as elements, or when there's no obvious pattern that relates elements to values/outputs.

(a) new map: for each element, you create a (element, value) pair in a Map. If the values don't relate to anything else, this may be appropriate...although that's probably pretty rare.

(b) an existing Map: often you'll have an existing lookup table that does what you need it to (see the note below); no need to create a new one.

(3) Element instance variable-backed. This is much the same as Map-backed but with a different storage model.

(4) Based on an on-the-fly function call (calculation, status report, etc.).

(5) Combinations or variants of the above, eg transformers that use picking information to determine which of two colors to use.

Note that in any of these cases, the transformer can take a process (map, instance variable, function call) which outputs something other than what you want (eg, a floating-point value) and translate it to a value of the appropriate type (eg, a Paint). For example, let's suppose that you want to paint vertices red if they have high PageRank, yellow if they have moderate PageRank, and black if they have low PageRank. You can easily construct a Transformer class that takes the PageRank data (probably itself provided by a Transformer which you provide to the constructor), figures out which of three intervals you want, and then outputs an appropriate color when you give it a vertex. Taking this a step further, it would even be pretty easy to write a threshold-based general Transformer that would take a Transformer from threshold values to colors as its constructor parameter.

This is really the key insight about using Transformers in JUNG: we're trying to use them in a way that means that you have as little work to do as possible in order to, say, run an algorithm where edge weights are based on the number of papers coauthored by the incident vertices (representing authors), or create a visualization for which vertex color is a function of activity level.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM