package graphs
This package defines graph algorithms as well as factory methods to describe and compute graphs and trees.
This package supports the following types of graphs:
- graphs based on explicitly connected nodes (org.opalj.graphs.Node),
- graphs where the relationship between the nodes are encoded externally (org.opalj.graphs.Graph).
- Source
- package.scala
- Alphabetic
- By Inheritance
- graphs
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Type Members
- abstract class AbstractDominatorTree extends AnyRef
Representation of a (post) dominator tree of, for example, a control flow graph.
Representation of a (post) dominator tree of, for example, a control flow graph. To construct a (post)dominator tree use the companion objects' factory methods (
apply
). - trait AbstractGraph[N] extends (N) => IterableOnce[N]
Represents a(n im)mutable (multi-)graph with (un)ordered edges.
- trait ControlDependencies extends AnyRef
Represents the control-dependence information.
Represents the control-dependence information.
An instruction/statement is control dependent on a predicate (here:
if
,switch
or any instruction that may throw an exception) if the value of the predicate controls the execution of the instruction.Note that the classical definition:
Let G be a control flow graph; Let X and Y be nodes in G; Y is control dependent on X iff there exists a directed path P from X to Y with any Z in P \ X is not post-dominated by Y.
Is not well suited for methods with potentially infinite loops, exceptions and multiple exit points. (See PostDominatorTree$.apply for further information.)
- Note
In the context of static analysis an instruction (e.g., invoke, idiv,...) that may throw an exception that results in a different control-flow, is also a
,predicate
additionally to all ifs and switches.If the underlying method/CFG contains infinite loops then it is expected that the dominance frontiers are already
corrected
if the used post dominator tree was augmented in the first place!
- class DefaultMutableNode[I] extends MutableNodeLike[I, DefaultMutableNode[I]] with MutableNode[I, DefaultMutableNode[I]]
Default implementation of a mutable node of a graph.
Default implementation of a mutable node of a graph.
Thread Safety
This is class is thread-safe.
- final class DominanceFrontiers extends ControlDependencies
Representation of the dominance frontiers.
- final class DominatorTree extends AbstractDominatorTree
A (standard) dominator tree.
A (standard) dominator tree.
- Note
Int => ((Int => Unit) => Unit)
is basically anIntFunction[Consumer[IntConsumer]]
.
- class Graph[N] extends AbstractGraph[N]
Represents a mutable (multi-)graph with ordered edges.
Represents a mutable (multi-)graph with ordered edges.
Thread Safety
This class is not thread-safe!
- trait MutableNode[I, N <: Node] extends Node
Common interface of all mutable nodes of a directed graph.
Common interface of all mutable nodes of a directed graph. This class basically serves as a small adapter class for some arbitrary node.
- I
The type of the object that is associated with this node/the type of the object for which this node object is created.
- N
The type of the node of the child nodes that can be added or removed.
- See also
The demo project for example usages.
- class MutableNodeLike[I, N <: Node] extends MutableNode[I, N]
Represents a mutable node of a directed graph.
Represents a mutable node of a directed graph. This class serves as a base implementation of the MutableNode trait.
Thread Safety
This class is thread-safe. It is possible to add multiple child nodes concurrently.
- I
The type of the object that is associated with this node/the type of the object for which this node object is created.
- See also
The demo project for example usages.
- trait Node extends AnyRef
Represents a node of some graph.
Represents a node of some graph.
Two nodes are considered equal if they have the same unique id.
- See also
org.opalj.br.ClassHierarchy's
toGraph
method for an example usage.
- final class PostDominatorTree extends AbstractDominatorTree
A representation of a post-dominator tree (see PostDominatorTree$#apply* for details regarding the properties).
A representation of a post-dominator tree (see PostDominatorTree$#apply* for details regarding the properties).
For information regarding issues related to using post-dominator trees for computing control dependence information see "A New Foundation for Control Dependence and Slicing for Modern Program Structures" (2007, Journal Version appeared in TOPLAS)
- class UnidirectionalGraph extends AbstractGraph[Int]
Efficient representation of a mutable graph where the nodes are identified using consecutive int values.
Efficient representation of a mutable graph where the nodes are identified using consecutive int values. This graph in particular supports the case where many nodes do not have successors. Computing the strongly connected components is particular efficient as no transformations are are required.
Thread Safety
This class is not thread-safe!
val g = new org.opalj.graphs.UnidirectionalGraph(10)() += (3,2) += (4,4) += (4,2) += (2, 4)
Example: - class VirtualUnidirectionalGraph extends AbstractGraph[Int]
Efficient representation of a mutable graph where the nodes are identified using consecutive int values (0,1,3,...).
Efficient representation of a mutable graph where the nodes are identified using consecutive int values (0,1,3,...). This graph in particular supports the case where many nodes do not have successors. Furthermore, computing the strongly connected components is particular efficient as no transformations are required since we already use int values for the nodes.
Thread Safety
This class is not thread-safe!
Some nodes may have no successors:
val edges = Map((0 -> List(1)),(1 -> List(0)),(2 -> List(3))/*,(3 -> List())*/) val successors : Int => Iterator[Int] = (i : Int) => { edges.get(i) match {case Some(successors) => successors.toIterator; case _ => Iterator.empty } } val vg = new org.opalj.graphs.VirtualUnidirectionalGraph(4/*max id of a node +1 */,successors)
Example:
Value Members
- def closedSCCs[N >: Null <: AnyRef](ns: Iterable[N], es: (N) => Iterable[N])(implicit arg0: ClassTag[N]): List[Iterable[N]]
Identifies closed strongly connected components in directed (multi-)graphs.
Identifies closed strongly connected components in directed (multi-)graphs.
A closed strongly connected component (cSCC) is a non-empty set of nodes of a graph where each node belonging to the cSCC can explicitly be reached from another node and no node contains an edge to some node that does not belong to the same cSCC.
Every such set is necessarily minimal/maximal.
- N
The type of the graph's nodes. The nodes have to correctly implements equals and hashCode.
- ns
The nodes of the graph.
- es
A function that, given a node, returns all successor nodes. Basically, the edges of the graph.
- Note
This implementation can handle (arbitrarily degenerated) graphs with up to Int.MaxValue nodes (if the VM is given enough memory!)
- final def closedSCCs[N >: Null <: AnyRef](g: Graph[N])(implicit arg0: ClassTag[N]): List[Iterable[N]]
- final lazy val dotToSVG: (String) => String
Function to convert a given graphviz dot file to SVG.
Function to convert a given graphviz dot file to SVG. The transformation is done using the vis-js.com library which is a translated version of graphviz to JavaScript.
The first call, which will initialize the JavaScript engine, will take some time. Afterwards, the tranformation is much faster.
- def sccs(ns: Int, es: (Int) => IntIterator, filterSingletons: Boolean = false): List[List[Int]]
Implementation of Tarjan's algorithm for finding strongly connected components.
Implementation of Tarjan's algorithm for finding strongly connected components. Compared to the standard implementation using non-tail recursive calls, this one uses an explicit stack to make the implementation scale to very large (degenerated) graphs. E.g., this implementation can handle graphs containing up to XXX nodes in a single cycle.
- ns
The number of nodes. The nodes have to be consecutively numbered [0..ns-1].
- es
A function that returns for a given node n the immediate successors for that node.
- filterSingletons
Removes SCCs with just one node, where the node is not connected to itself. I.e., nodes which have a self-edge will be kept and other will be discarded.
A very simple, but very large cycle:
def genGraph(max : Int) = { var i = 1; var g = Map[Int,List[Int]]((0,List(max-1))); while(i < max){ g += ((i,List(i-1))); i+=1; } g } val g = genGraph(100000) val es = (i:Int) => {g(i).toIterator} org.opalj.graphs.sccs(g.size,es).mkString("\n")
A large graph:
val g = Map((0,List(5)),(1,List(2)),(2,List(1,4)),(3,List(0)),(4,List(2)),(5,List(4,3,6)),(6,List(6)),(7, List())) val es = (i:Int) => { g(i).toIterator } org.opalj.graphs.sccs(g.size,es,filterSingletons = true).mkString("\n")
Example: - def toAdjacencyMatrix(maxNodeId: Int, successors: (Int) => Set[Int]): Array[Byte]
Returns the given graph as a CSV encoded adjacency matrix.
Returns the given graph as a CSV encoded adjacency matrix.
- maxNodeId
The id of the last node. The first node has to have the id 0. I.e., in case of a graph with just two nodes, the maxNodeId is 1.
- successors
The successor nodes of the node with the given id; the function has to be defined for every node in the range [0..maxNodeId].
- returns
an adjacency matrix describing the given graph encoded using CSV. The returned byte array an be directly saved and represents a valid CSV file.
For example, the graph p with the nodes A (id = 0),B (id = 1),C (id =2) and
- an edge from A to B,
- an edge from A to C and
- an edge from C to B
would be encoded as follows:
0,1,1 0,0,0 0,1,0
- Note
Though the function is optimized to handle very large graphs, encoding sparse graphs using adjacency matrixes is not recommended.
Example: - def toDot(rootNodes: Iterable[_ <: Node], dir: String = "forward", ranksep: String = "0.8", fontname: String = "Helvetica", rankdir: String = "TB"): String
Generates a string that describes a (multi-)graph using the ".dot/.gv" file format http://graphviz.org/pdf/dotguide.pdf.
Generates a string that describes a (multi-)graph using the ".dot/.gv" file format http://graphviz.org/pdf/dotguide.pdf. The graph is defined by the given set of nodes.
Requires that
Node
implements a content-basedequals
andhashCode
method. - object DefaultMutableMode
- object DominanceFrontiers
Factory to compute DominanceFrontiers.
- object DominatorTree
Factory to compute DominatorTrees.
- object Graph
Defines factory methods to create simple graphs.
- object PostDominatorTree
Factory for post dominator trees.