[[graph-algo]]
Graph Algorithms
================

Neo4j graph algorithms is a component that contains Neo4j implementations of some common algorithms for graphs.
It includes algorithms like:

* Shortest paths,
* all paths,
* all simple paths,
* Dijkstra and
* A*.

[[graph-algo-introduction]]
== Introduction ==

The graph algorithms are found in the +neo4j-graph-algo+ component, which is included in the standard Neo4j download.

* http://components.neo4j.org/neo4j/{neo4j-version}/apidocs/org/neo4j/graphalgo/package-summary.html[Javadocs]
* http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.neo4j%22%20AND%20a%3A%22neo4j-graph-algo%22[Download]
* https://github.com/neo4j/community/tree/master/graph-algo[Source code]

For information on how to use neo4j-graph-algo as a dependency with Maven and other dependency management tools, see +http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.neo4j%22%20AND%20a%3A%22neo4j-graph-algo%22[org.neo4j:neo4j-graph-algo]+
Note that it should be used with the same version of +http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.neo4j%22%20AND%20a%3A%22neo4j-kernel%22[org.neo4j:neo4j-kernel]+.
Different versions of the graph-algo and kernel components are not compatible in the general case.
Both components are included transitively by the +http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.neo4j%22%20AND%20a%3A%22neo4j%22[org.neo4j:neo4j]+ artifact which makes it simple to keep the versions in sync.

The starting point to find and use graph algorithms is +http://components.neo4j.org/neo4j/{neo4j-version}/apidocs/org/neo4j/graphalgo/GraphAlgoFactory.html[GraphAlgoFactory]+.

[[graph-algo-path-finding]]
== Path finding examples ==

Calculating the shortest path (least number of relationships) between two nodes:

[snippet,java]
----
component=neo4j-graph-algo
source=examples/PathFindingExamplesTest.java
tag=shortestPathUsage
----

Using http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm[Dijkstra's algorithm] to calculate cheapest path between node A and B where each relationship can have a weight (i.e. cost) and the path(s) with least cost are found.

[snippet,java]
----
component=neo4j-graph-algo
source=examples/PathFindingExamplesTest.java
tag=dijkstraUsage
----

Using http://en.wikipedia.org/wiki/A*_search_algorithm[A*] to calculate the cheapest path between node A and B, where cheapest is for example the path in a network of roads which has the shortest length between node A and B.
Here's our example graph:

image::graphalgo-astar.png[scaledwidth="50%", alt="A* algorithm example graph"]

[snippet,java]
----
component=neo4j-graph-algo
source=examples/PathFindingExamplesTest.java
tag=astarUsage
----

The full source code of the path finding examples are found at https://github.com/neo4j/graphdb/blob/master/graph-algo/src/test/java/examples/PathFindingExamplesTest.java.

///////////////////



Other algos

	There are other algorithms which can be used on smaller graphs, f.ex. calculating
	centrality, betweeness, closeness, eccentrity and more. Those algos aren't designed
	to scale to very big graphs, but can still be useful in some scenarios. They reside in the
	<<org.neo4j.graphalgo.impl.centrality>> package.

///////////////////

