networkx - Find All paths in a Directed Cyclic Graph -


actually working on text based directed graph having each word node in graph , edges between 2 adjacent words in sentence of text.

i need find paths start node end node.

is there library python can me task?

i tried doing networkx, problem networkx outputs simple paths (simple paths pretty short long sentence in input , don't contain information of sentence). , need more complex paths task.

you need write algorithm dfs(depth first search) or bfs(breadth first search) collect paths. below example collect possible paths source destination written in java.

    package com.nirav.modi;      import java.util.arraylist;     import java.util.collections;     import java.util.hashmap;     import java.util.iterator;     import java.util.linkedhashset;     import java.util.list;     import java.util.map;     import java.util.nosuchelementexception;     import java.util.set;      class graph<t> implements iterable<t> {          /*          * map nodes in graph sets of outgoing edges. each set of          * edges represented map edges doubles.          */         private final map<t, map<t, double>> graph = new hashmap<t, map<t, double>>();          /**          * adds new node graph. if node exists          * no-op.          *           * @param node          *            adds graph. if node null no-op.          * @return true if node added, false otherwise.          */         public boolean addnode(t node) {             if (node == null) {                 throw new nullpointerexception("the input node cannot null.");             }             if (graph.containskey(node))                 return false;              graph.put(node, new hashmap<t, double>());             return true;         }          /**          * given source , destination node add arc source          * destination node. if arc exists value          * updated new value.          *           * @param source          *            source node.          * @param destination          *            destination node.          * @param length          *            if length if          * @throws nullpointerexception          *             if source or destination null.          * @throws nosuchelementexception          *             if either source of destination not exists.          */         public void addedge(t source, t destination, double length) {             if (source == null || destination == null) {                 throw new nullpointerexception("source , destination, both should non-null.");             }             if (!graph.containskey(source) || !graph.containskey(destination)) {                 throw new nosuchelementexception("source , destination, both should part of graph");             }             /* node added no point returning true or false */             graph.get(source).put(destination, length);         }          /**          * removes edge graph.          *           * @param source          *            if source node.          * @param destination          *            if destination node.          * @throws nullpointerexception          *             if either source or destination specified null          * @throws nosuchelementexception          *             if graph not contain either source or destination          */         public void removeedge(t source, t destination) {             if (source == null || destination == null) {                 throw new nullpointerexception("source , destination, both should non-null.");             }             if (!graph.containskey(source) || !graph.containskey(destination)) {                 throw new nosuchelementexception("source , destination, both should part of graph");             }             graph.get(source).remove(destination);         }          /**          * given node, returns edges going outward node, immutable          * map.          *           * @param node          *            node edges should queried.          * @return immutable view of edges leaving node.          * @throws nullpointerexception          *             if input node null.          * @throws nosuchelementexception          *             if node not in graph.          */         public map<t, double> edgesfrom(t node) {             if (node == null) {                 throw new nullpointerexception("the node should not null.");             }             map<t, double> edges = graph.get(node);             if (edges == null) {                 throw new nosuchelementexception("source node not exist.");             }             return collections.unmodifiablemap(edges);         }          /**          * returns iterator travels nodes of graph.          *           * @return iterator travels nodes of graph.          */         @override         public iterator<t> iterator() {             return graph.keyset().iterator();         }     }      /**      * given connected directed graph, find paths between 2 input      * points.      */     public class graphtester<t> {          private final graph<t> graph;          /**          * takes in graph. graph should not changed client          */         public graphtester(graph<t> graph) {             if (graph == null) {                 throw new nullpointerexception("the input graph cannot null.");             }             this.graph = graph;         }          private void validate(t source, t destination) {              if (source == null) {                 throw new nullpointerexception("the source: " + source + " cannot  null.");             }             if (destination == null) {                 throw new nullpointerexception("the destination: " + destination + " cannot  null.");             }             if (source.equals(destination)) {                 throw new illegalargumentexception("the source , destination: " + source + " cannot same.");             }         }          /**          * returns list of paths, path list of nodes.          *           * @param source          *            source node          * @param destination          *            destination node          * @return list of paths          */         public list<list<t>> getallpaths(t source, t destination) {             validate(source, destination);              list<list<t>> paths = new arraylist<list<t>>();             recursive(source, destination, paths, new linkedhashset<t>());             return paths;         }          // far dude ignore's cycles.         private void recursive(t current, t destination, list<list<t>> paths, linkedhashset<t> path) {             path.add(current);              if (current == destination) {                 paths.add(new arraylist<t>(path));                 path.remove(current);                 return;             }              final set<t> edges = graph.edgesfrom(current).keyset();              (t t : edges) {                 if (!path.contains(t)) {                     recursive(t, destination, paths, path);                 }             }              path.remove(current);         }          public static void main(string[] args) {             graph<string> graphfindallpaths = new graph<string>();             graphfindallpaths.addnode("a");             graphfindallpaths.addnode("b");             graphfindallpaths.addnode("c");             graphfindallpaths.addnode("d");              graphfindallpaths.addedge("a", "b", 10);             graphfindallpaths.addedge("a", "c", 10);             graphfindallpaths.addedge("b", "d", 10);             graphfindallpaths.addedge("c", "d", 10);              graphfindallpaths.addedge("b", "c", 10);             graphfindallpaths.addedge("c", "b", 10);              graphtester<string> findallpaths = new graphtester<string>(graphfindallpaths);             list<list<string>> allpaths = findallpaths.getallpaths("a", "d");             system.out.println(allpaths);              // assertequals(paths, findallpaths.getallpaths("a", "d"));         }     } 

Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -