Rectangle 27 46

While these answers are all correct, resolving the problem is often more difficult. It's generally the result of two mildly different versions of the same dependency on the classpath, and is almost always caused by either a different superclass than was originally compiled against being on the classpath or some import of the transitive closure being different, but generally at class instantiation and constructor invocation. (After successful class loading and ctor invocation, you'll get NoSuchMethodException or whatnot.)

If the behavior appears random, it's likely the result of a multithreaded program classloading different transitive dependencies based on what code got hit first.

To resolve these, try launching the VM with -verbose as an argument, then look at the classes that were being loaded when the exception occurs. You should see some surprising information. For instance, having multiple copies of the same dependency and versions you never expected or would have accepted if you knew they were being included.

Resolving duplicate jars with Maven is best done with a combination of the maven-dependency-plugin and maven-enforcer-plugin under Maven (or SBT's Dependency Graph Plugin, then adding those jars to a section of your top-level POM or as imported dependency elements in SBT (to remove those dependencies).

compiler construction - What causes java.lang.IncompatibleClassChangeE...

java compiler-construction jar runtime-error binary-compatibility
Rectangle 27 1

This is not so much a clustering problem, but you have your neighbor relation, and you want to compute the transitive closure of this neighbor relation.

This is a much simpler problem, and it has an obvious and efficient straightforward solution (breadth-first search):

While there are unprocessed points:

  • Initialize a new empty result set.
  • Mark all points in the working set as processed
  • Working set = all ''unprocessed'' neighbors of the previous working set
  • Return the result set as new group.

It seems somewhat disrespectful and contrary to the spirit of the site to claim that the problem has an "obvious and straightforward" solution and then not give that solution.

machine learning - Cluster adjacent points - Stack Overflow

machine-learning cluster-analysis data-mining
Rectangle 27 1

Problem solved using networkx in Python. networkx provides a functionality to find all sub-graphs of a given one.

All I needed is to have the output of the transitive_closure() method, translate it to a graph for networkx and then having the new created graph as an input to the connected_component_subgraphs() method provided by networkx.


The main drawback is processing time, but that's the best I could find.

Find all Subgraphs using the reflexive transitive closure in Python - ...

python python-3.x python-2.7
Rectangle 27 2

The first thing that you're talking about here (generating all edges like a->c when a->b and b->c) is called a transitive closure. It is itself useful, interesting and studied. However, storing all such edges explicitly will result in a (potentially quadratic) blowup of the storage space required for the graph, because in a complete graph with |V| nodes you have O(|V|2) edges. So this is a trade-off between space and time complexity: if you store all the (forward) edges you can traverse the graph (forward) much more quickly, in constant time as you observed, but you pay the price in storage.

Although you didn't ask about this, I'll point out that storing the transitive closure explicitly is probably undesirable for dependency graphs. Take for instance a package manager: you want it to find out quickly the immediate dependencies in order to check if they are installed and if they are not perhaps to add the missing ones to a transaction installing multiple packages. But enabling constant time access to all (direct and indirect) dependencies of a package doesn't seem particularly useful in this context because most of the indirect dependencies are likely to be satisfied anyway. You'd just get a much larger list to go over and probably conclude that most are installed anyway.

The other thing that you're talking about, i.e the graph with every edge reversed, is called the transpose graph. Note that you need to [bi]color the edges (use differently named member pointer/references) if you store both the "direct" and the transpose graph in the same data structure. Storing them together this way is rather trivial so I guess that's why you don't see much mention of it. Some graph algorithm works/books do assume such a representation for directed graphs, i.e. that both the incoming and outgoing edges are stored in separate (doubly linked) lists for each vertex. Although many (introductory) textbooks indeed don't talk about it (presumably in order to keep their presentation simple), this representation (i.e. with both incoming and outgoing lists) is used in practice, for example in LEDA. Here's a slide from a LEDA presentation detailing their static (i.e. assumed fixed) graph data structures; the dynamic one would have doubly-linked lists instead of arrays. I'm including both the unidirectional ("directed") and their "bidirectional" representation for easy comparison:

Boost has a similar feature, although it is just a tweak (called bidirectionalS) for their adjacency list implementation:

The bidirectionalS selector specifies that the graph will provide the in_edges() function as well as the out_edges() function. This imposes twice as much space overhead per edge, which is why in_edges() is optional.

Keep in mind that because you can tell apart the two sets of edges (via "first in" and "first out" in the case of LEDA or in_edges() and out_edges in the case of boost) what you really have here is a mathematically a disjoint union of a directed graph with its transpose. If you lose the distinction/color between the two sets of edges (pointers), what you get is sometimes called a bidirected graph, although the term (like alas many in graph theory) is unfortunately overloaded. And if you had hopes that LEDA's term bidirectional graph is somehow standardized with their meaning, it's actually more likely to mean the same thing as bidirected graph to theorists.

As for practical representations of package repositories (3), you seem to ignore that most (that I know of) will store AND, OR and NOT constraints to additionally handle alternatives and conflicts. You can only handle AND with a dependency graph like we discussed above. Once you add those additional OR & NOT features you get to solve much harder (NP-complete) SAT problems just to install something; see the Opium paper (2007) for a discussion; for a more recent one (2010) see the Apt-pbo paper. So constant-time reverse dependency look-ups start to seem like a trifle in comparison. But to actually answer your question:

  • I've looked at the apt sources and it does store the reverse dependencies separately in its cache (which you query with apt-cache). For each package (pkgCache::Package defined in pkgcache.h) there's a RevDepends linked list and apt updates it every time you install or remove something: in it has for loop that does (among other things) an Update(P.ParentPkg().RevDependsList());.

algorithm - Why are dependency graphs not represented as bi-directiona...

algorithm data-structures graph dependencies
Rectangle 27 3

Here is an up to date, Maven3 example on how to get all dependencies (including transitive) as well as have access to the files themselves (if you for instance need to add the paths to a classpath).

// Default phase is not necessarily important.
// Both requiresDependencyCollection and requiresDependencyResolution are extremely important however!
@Mojo(name = "simple", defaultPhase = LifecyclePhase.PROCESS_RESOURCES, requiresDependencyCollection = ResolutionScope.COMPILE_PLUS_RUNTIME, requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME)
public class SimpleMojo extends AbstractMojo {
  @Parameter(defaultValue = "${project}", readonly = true)
  private MavenProject mavenProject;

  public void execute() throws MojoExecutionException, MojoFailureException {
    for (final Artifact artifact : mavenProject.getArtifacts()) {
      // Do whatever you need here.
      // If having the actual file (artifact.getFile()) is not important, you do not need requiresDependencyResolution.

Changing the parameters in the Mojo is a very important piece that I was missing. Without it, lines like the following:

@Parameter(defaultValue = "${project.compileClasspathElements}", readonly = true, required = true)
private List<String> compilePath;

Will only return the classes directory, not the path you expect.

Changing the requiresDependencyCollection and requiresDependencyResolution to different values will allow you to change the scope of what you want to grab. The maven documentation can provide more detail.

How to get access to Maven's dependency hierarchy within a plugin - St...

maven maven-plugin maven-dependency-plugin aether
Rectangle 27 1

A common catch-all approach seems to be to build an adjacency matrix for the data, then use matrix multiplications to build a kind of transitive closure, then e.g. compute the inverse matrix to identify a "reason", or PCA/ICA to identify groups.

Generating clusters from adjacency matrix / edge list in R - Stack Ove...

r cluster-analysis adjacency-list
Rectangle 27 0

You could consider adding a closure table that contains all the paths from each of the tree roots to the leaves. Maintaining the transitive closure of graphs in SQL (from 1999) describes some of the theoretical background.

The stackoverflow review question on Hierarchical data describes a number of the alternative approaches. In there, Tegiri Nenashi points to a comprehensive bibliography that includes Hierarchical data in RDBMSs.

Closure table have the advantage that the queries are efficient, and the solution does not affect your current data structure. You will need to extend your with the closure table and maintain it when the forest is modified.

You indicate a large number of items in the table with short paths. This makes the closure table performance remain nearly linear with appropriate indices. You can also retain the path to each node in the close table to avoid recalculation. The approach has a constant number of queries for each operation, and supports hierarchies of any depth.

mysql - How to find the hierarchy path for a tree representation - Sta...

mysql sql hierarchical-data
Rectangle 27 0

I can't really think of a better way. The only change I would make is to use EXISTS instead of IN:

SELECT c.*, p.*
FROM comments AS c
JOIN comment_paths AS p
ON = p.child_id
(SELECT * FROM comments c2 WHERE p.parent_id = AND c2.parent_id IS NULL AND c2.post_id = 6)

I'm interested to see if there actually is a better method though.

I'm not sure what RDMS you're using. But, if recursive queries are available, I'm thinking you could get away from the comments_path table entirely:

;with cte as(
select c.*, 0 as depth
from comments c
where c.post_id = 6 and c.parent_id is null
union all
select c.*, cte.depth + 1
from comments c
join cte on c.parent_id =

select * from cte

Thanks for your answer. Is there any reason to use EXISTS versus IN? Substituting it returns a very different data set. It looks like it does a join on every record in the comment_paths set instead of on the comments set.

The different result set was due a typo in the join (I had " = p.parent_id" instead of " = p.child_id"). In my experience, EXISTS performs better than IN. In certain instances, there may not be a performance boost, but it should always perform as well as, if not better than IN.

I just saw your recursive queries update. That's definitely a route I would like to take with an RDMS that provides it should it be faster than a closure table, but I'd like my project to be able to run on as many RDMS as possible and avoid RDMS-specific queries just to keep bugs to a minimum. Thanks so much for the update, though!

sql - Running a SELECT query on a closure table with a JOIN? - Stack O...

sql hierarchical-data transitive-closure-table
Rectangle 27 0

So you can't use lists (I wonder why) but can you use a counter variable? Try iteratively deepening search where you do depth-first search first in the depth of 1, then 2, and so on. That would prevent the infinite loops with cycles.

Remember to have an upper limit for search depth to avoid infinite looping in case where there is no connection.

Prolog Infinite loop (cyclic graph) - Stack Overflow

graph prolog infinite-loop transitive-closure
Rectangle 27 0

I think this is not really it, but anyway:

use strict;
use warnings;
my @rows;
my %indx;
while(<DATA>) {
  my @v = split (/\s+/);
  my $r = {};
  for my $k (@v) {
    $r = $indx{$k}[0] if defined $indx{$k};
  # print join(",", @v), "\n";
  push(@{$indx{$v[0]}}, $r);
  push(@{$indx{$v[1]}}, $r);
  push(@rows,  $r);
my %seen;
for my $r (@rows) {
  print (join("\t", keys %$r), "\n") if not $seen{$r}++;


awk - Compute transitive closure - Stack Overflow

awk bioinformatics dna-sequence transitive-closure
Rectangle 27 0

If I understand your scenario correctly, then yes it's easy to do this. Just add an options closure to the end of the dependency declaration to prevent transitive dependencies (I've changed A,B,C .jar to X,Y,Z because I'm guessing they don't coincide with projects A and B):

// Project A build.gradle
dependencies {
   compile(files('X.jar', 'Y.jar')) { transitive = false }
   export files('Z.jar')

Which would prevent X.jar and Y.jar from being added to the classpath for project B.

Alternatively, and I don't know how well this would work for you and don't really recommend it (just want you to know of the possibilities) you could do this in project B's build.gradle:

configurations.compile.dependencies.find { == "A.jar" }.exclude(jar: it)
configurations.compile.dependencies.find { == "B.jar" }.exclude(jar: it)

I had already tried that and it did not work. Now I digged a little and it turns out that the transitive property is only available for module dependencies, but not for file collection dependencies. So you you could say that this is a missing feature as of 1.0-milestone 6. Maybe you can change the first part of your answer to reflect this, then I can accept it.

I'll do you one better. I'm talking to the founder of Gradle in about 5 hours and I'll ask him about this quick.

@EricWendelin So what did you find out?

java - Transitive file dependencies in gradle - Stack Overflow

java gradle
Rectangle 27 0

Here is a universal converter that uses a hollow matrix, and that does a transitive closure to combine known converters. In this case, it converts length units:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Converter {
    private static final Map<String, Map<String, Double>> FACTOR_MAP
            = new HashMap<String, Map<String, Double>>();

    public static double convert(String from, String to, double value) {
        List<Step> stack = new ArrayList<Step>();
        stack.add(new Step(from, value));
        while (!stack.isEmpty()) {
            Step s = stack.remove(0);
            double val = s.value;
            String source = s.unit;
            if (source.equals(to)) {
                return val;
            Map<String, Double> map = FACTOR_MAP.get(source);
            if (map != null) {
                for (Map.Entry<String,Double> entry: map.entrySet()) {
                    stack.add(new Step(entry.getKey(), val*entry.getValue()));
        throw new IllegalArgumentException("Cannot not convert from " + from
                + " to " + to);

    public static void registerFactor(String from, String to, double factor) {
        putFactor(from, to, factor);
        putFactor(to, from, 1.0/factor);

    private static void putFactor(String from, String to, double factor) {
        Map<String, Double> map = FACTOR_MAP.get(from);
        if (map == null) {
            map = new HashMap<String, Double>();
            FACTOR_MAP.put(from, map);
        map.put(to, factor);

    static {
        registerFactor("cm", "mm", 10);
        registerFactor("in", "cm", 2.54);
        registerFactor("in", "pt", 72);
        registerFactor("pc", "pt", 12);
        registerFactor("px", "mm", 0.28);

    private static class Step {
        private String unit;
        private double value;

        Step(String unit, double value) {
            this.unit = unit;
            this.value = value;

The following program:

public class Main {
    private static final String UNITS[] = {"cm", "mm", "in", "pt", "pc", "px"};

    public static void main(String[] args) {
        for (String unit1: UNITS) {
            for (String unit2: UNITS) {
                System.out.println("1" + unit1 + " = "
                        + Converter.convert(unit1, unit2, 1) + unit2);

gives the following results:

1cm = 1.0cm
1cm = 10.0mm
1cm = 0.39370078740157477in
1cm = 28.346456692913385pt
1cm = 2.3622047244094486pc
1cm = 35.71428571428571px
1mm = 0.1cm
1mm = 1.0mm
1mm = 0.03937007874015748in
1mm = 2.8346456692913384pt
1mm = 0.23622047244094485pc
1mm = 3.571428571428571px
1in = 2.54cm
1in = 25.4mm
1in = 1.0in
1in = 72.0pt
1in = 6.0pc
1in = 90.71428571428571px
1pt = 0.035277777777777776cm
1pt = 0.35277777777777775mm
1pt = 0.013888888888888888in
1pt = 1.0pt
1pt = 0.08333333333333333pc
1pt = 1.2599206349206347px
1pc = 0.42333333333333334cm
1pc = 4.233333333333333mm
1pc = 0.16666666666666666in
1pc = 12.0pt
1pc = 1.0pc
1pc = 15.119047619047619px
1px = 0.028000000000000004cm
1px = 0.28mm
1px = 0.011023622047244094in
1px = 0.7937007874015748pt
1px = 0.06614173228346457pc
1px = 1.0px

I like this one. But, don't you think there is a dependency on all these string definitions now? Yes, ofcourse they can be externalized, but the idea is to leave the design open, so that you don't have to modify existing code to add new behavior. For instance, if I were to add a new "factor definition", I would like to do this by adding a new class and sending it in as a param to my program for conversion.

This is just a example. Nothing prevents you from using anything else than strings (event plain Objects).

How to extend this design for a generic converter in java? - Stack Ove...

java design design-patterns generics oop
Rectangle 27 0

Related Topics:


Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Writing code in comment?

Rectangle 27 0

  • First, in order to find what vertexes participate in cycles, do a transitive closure. It's a O(V^3) algorithm.
  • Remove all the other vertexes.
  • The solution for it is - "maximal pair matching in bipartitive graph" algorithm.

4.1. Turn each vertex v in your graph (G) into 2 (v1 and v2), place each in different parts of bipartitive graph (G2).

4.2. For each edge e(v,u) in G, add an edge from 1st part of G2 to 2nd - e(v1,u2).

4.3. Find a maximal pair matching in G2. It's a subset of G2 edges.

5 That subset corresponds to a maximal (full) set of independent loops in G.

algorithm - Enumerating All Minimal Directed Cycles Of A Directed Grap...

algorithm cycle directed-graph
Rectangle 27 0

I wrote a tool called auto-builder: It introspects PDE-based projects and generates Ant build files; it supports transitive closure over dependencies and all that jazz.

I posted a write-up: I wrote it because the Maven tools I played with, when integrated with PDE, didnt just work. Basically, I wanted to do coding in PDE and have a Hudson-based CI without any fuss in between.

Generating Ant files is nice because it gives you all the benefits of a declarative build tool, but it leaves you with a procedural description of what it is doing.

I am looking for more PDE-based projects to test it on. There are a couple RFC-0112 Bundle repositories around, and I have some code for downloading dependencies. If anyone is interested, then I could integrate dependencies download with auto-builder.

Maven : OSGI, bundles and multi-modules projects - Stack Overflow

maven-2 osgi dependencies
Rectangle 27 0

Maybe I've misunderstood; but could you not use an extension method to add the ability to navigate this way? You'd still need to call a function to get the quotes for the particular contract, but the helper would exist, which can just call your data provider for a filtered list of quotes for that contact?

that seems to be a practical and no nonsense idea. as I was (re)picking up programming at the time of the question, I was asking the question in a more general way which is probably not fit for SO anyway.

c# - Class hierarchy definition VS (automatically) generated navigatio...

c# entity-framework f# navigation-properties transitive-closure
Rectangle 27 0

I have come across similar situations many a times while starting to write some piece of code. I think, you have a pretty good idea of your problem. You can make a sample text file ( even on a piece of paper ) with 2-3 values and try to think more about how you want things to be. You know that there need to be a main method that takes your file name. You can use a scanner in the main or in constructor or in any other method. This depends on the design you use. If you want a very basic program you can declare it in the main method and go ahead. But I suggest you should use another method to take input from the file, something like readInputFromText(), where you declare a local scanner object. You can ofcourse do this in other ways too. Some basic rules I follow are :

Have separate methods for each task. To open a file, I have a fileOpener, to close it, I have another method etc. Each method is supposed to do a single task only. All functions related to file will be put in a single class say, FileUtil. This is a utility task and not your main task, so you keep all these util classes, like FileUtilities, StringUtilities, DatabaseUtilites etc. in a Util package.

I don't know if it really answers your question in someway. But keep these in mind and start off your program. Test at each stage and make a primary version. Keep the improvements for subsequent versions.

If you have specific questions, a big community is waiting for you :)

java - How to start off from these details - Stack Overflow

java map floyd-warshall transitive-closure
Rectangle 27 0

select h1.descendant intermediate_node
from hierarchy h0 
join hierarchy h1 
  on h0.ancestor = h1.ancestor 
 and h0.hops > h1.hops  -- redundant condition, but may improve performance
join hierarchy h2
  on h1.ancestor = h2.ancestor 
 and h0.descendant = h2.descendant
where h0.ancestor = :ancestor and h0.descendant = :descendant

sql - How do I query for all the nodes between two nodes in a tree? - ...

sql hierarchical-data hierarchical-trees hierarchical-query transitive-closure-table
Rectangle 27 0

I came across your question because I am looking at closure tables as well. I think your problem can be solved if you follow Bill Karwin's advice on closure tables in his book "Sql anti-patterns":

Store one row in this table for each pair of nodes in the tree that shares an ancestor/descendant relationship, even if they are separated by multiple levels in the tree.

Thus, one query on parentId would produce all children of the parent in one shot. (In SQL server this could be done by a recursive query).

You can improve the Closure Table to make queries for immediate parent or child nodes easier. Add a TreePaths.path_length attribute to the Closure Table design.

That is: store the distance between items in the closure table.

I think you'd really like his example. It's about comment threads as well.

mysql - Closure Table the best SELECT QUERY - Stack Overflow

mysql transitive-closure-table
Rectangle 27 0

DBSCAN uses the transitive closure.

Therefore, the distance of points in one cluster can be much larger than epsilon. It guarantees there exists a sequence a,b,c,d,e,f...,x such that every step is at most epsilon, but there is no limit on the number of steps.

You could try Leader clustering, but I would rather not use clustering at all, but treat this as a simpler similarity search problem - you want to find similar objects, not complex structured groups.

python - Buggy results using DBSCAN (sklearn) for word clustering - St...

python scikit-learn dbscan