Skip to content

Commit b880f41

Browse files
committed
merge
2 parents 501ff3e + 243dcf6 commit b880f41

File tree

3 files changed

+138
-78
lines changed

3 files changed

+138
-78
lines changed

.test

Whitespace-only changes.

CONTRIBUTING.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@
33
### guidelines
44
* please fork and propose a pull request with `github.com`
55
* please follow the code conventions and style.
6+
7+
### wishlist
8+
* binary operations on graphs such as merging two graphs into one new happy graph.
9+
* factory methods for bulk generation of nodes.
10+
* more graph engines, for example an engine that is based on the adjacency matrix
611
* unit testing contributions will be awesome.
712

813
### Contact Author
914
1015
* [Google+ TomershalevMan](https://plus.google.com/+TomershalevMan/about)
11-
* [Facebook - HendrixString](https://www.facebook.com/HendrixString)
16+
* [Facebook - HendrixString](https://www.facebook.com/HendrixString)

README.md

Lines changed: 132 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Add to your dependencies:
2121

2222
```groovy
2323
dependencies {
24-
compile 'com.github.HendrixString:Android-PdfMyXml:{Tag}' // the latest version is "v1.0.1"
24+
compile 'tbd'
2525
}
2626
```
2727

@@ -75,7 +75,7 @@ with the correct abstractions and utilities. The builtin algorithms are:
7575
* [Johnson's algorithm](https://en.wikipedia.org/wiki/Johnson's_algorithm)
7676
* minimum spanning tree
7777
* [Prim's algorithm](https://en.wikipedia.org/wiki/Prim's_algorithm)
78-
* [Kruskal's algorithm](hhttps://en.wikipedia.org/wiki/Kruskal's_algorithm)
78+
* [Kruskal's algorithm](https://en.wikipedia.org/wiki/Kruskal's_algorithm)
7979
* transformations
8080
* Square of a graph
8181
* transitive closure of a graph
@@ -84,99 +84,154 @@ with the correct abstractions and utilities. The builtin algorithms are:
8484
## builtin graph engines
8585
* **Adjacency** and **Incidence** list based graph engine <br/>designed for optimal complexity for algorithms that require more than a moderate edge queries.
8686

87-
### Instructions
88-
#### 1. create XML layouts
89-
First create XML layouts. give it dimensions in **pixels** (and for all it's sub views) and proportions according landscape or portrait according to ratio **1:1.41**.<br/><br/>
90-
page1.xml
87+
### Instructions, code by examples
88+
#### 1. creating a very simple graph
89+
9190
```java
92-
<?xml version="1.0" encoding="utf-8"?>
93-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
94-
android:layout_width="2115px"
95-
android:layout_height="1500px"
96-
android:background="@color/white">
97-
<TextView android:id="@+id/tv_hello"
98-
android:textColor="@color/black"
99-
android:textSize="27px"
100-
android:textStyle="bold"
101-
android:padding="6px"/>
102-
103-
</RelativeLayout>
104-
```
91+
SimpleDirectedGraph graph_triangle = new SimpleDirectedGraph();
10592

106-
you can create as many as pages/templates as you need.
93+
Vertex v0 = new Vertex();
94+
Vertex v1 = new Vertex();
95+
Vertex v2 = new Vertex("tag_v2");
10796

108-
#### 2. Implement a View renderer
109-
implement your View renderer by extending `AbstractViewRenderer` or by anonymously instantiating it and injecting the layout id. the initView(View view) will supply you an inflated View automatically. There are other options but I wont cover it now.
110-
```java
111-
AbstractViewRenderer page = new AbstractViewRenderer(context, R.layout.page1) {
112-
private String _text;
97+
graph_triangle.addVertex(v0);
98+
graph_triangle.addVertex(v1);
99+
graph_triangle.addVertex(v2);
113100

114-
public void setText(String text) {
115-
_text = text;
116-
}
101+
Edge e_0 = graph_triangle.addEdge(v0, v1);
102+
graph_triangle.addEdge(v1, v2);
103+
graph_triangle.addEdge(v2, v3);
117104

118-
@Override
119-
protected void initView(View view) {
120-
TextView tv_hello = (TextView)view.findViewById(R.id.tv_hello);
121-
tv_hello.setText(_text);
122-
}
123-
};
105+
graph_triangle.print();
106+
107+
// iterate the graph vertices directly
108+
for (IVertex vertex : graph_triangle) {
109+
System.out.println(vertex.toString());
110+
}
111+
112+
// iterate the edges of the graph
113+
for (Edge edge : graph_triangle.edges()) {
114+
System.out.println(edge.toString());
115+
}
124116

125-
// you can reuse the bitmap if you want
126-
page.setReuseBitmap(true);
117+
// removing a vertex in any of the following ways will remove it's connected edges as well,
118+
// also removing any edge in similar fashion will update the graph :)
119+
graph_triangle.removeVertex(v0);
120+
graph_triangle.vertices().remove(v1);
121+
graph_triangle.vertices().iterator().remove();
127122

128123
```
129124

130-
#### 3. Build the PDF document
131-
Use `PdfDocument` or `PdfDocument.Builder` to add pages and render and run it all at background with progress bar.
125+
#### 2. use a factory for custom graph
126+
you can define your graph in terms of self loops, multi edges (per vertex) and
127+
a custom implementation of a graph engine.
132128
```java
133-
PdfDocument doc = new PdfDocument(ctx);
134-
135-
// add as many pages as you have
136-
doc.addPage(page);
137-
138-
doc.setRenderWidth(2115);
139-
doc.setRenderHeight(1500);
140-
doc.setOrientation(PdfDocument.A4_MODE.LANDSCAPE);
141-
doc.setProgressTitle(R.string.gen_please_wait);
142-
doc.setProgressMessage(R.string.gen_pdf_file);
143-
doc.setFileName("test");
144-
doc.setInflateOnMainThread(false);
145-
doc.setListener(new PdfDocument.Callback() {
146-
@Override
147-
public void onComplete(File file) {
148-
Log.i(PdfDocument.TAG_PDF_MY_XML, "Complete");
149-
}
129+
boolean allow_self_loops = true;
130+
boolean allow_multi_edges = true;
150131

151-
@Override
152-
public void onError(Exception e) {
153-
Log.i(PdfDocument.TAG_PDF_MY_XML, "Error");
154-
}
155-
});
132+
UndirectedGraph graph_undirected = Erdos.newUndirectedGraphWithEngine(new AdjIncidenceGraphEngine(),
133+
allow_self_loops, allow_multi_edges);
156134

157-
doc.createPdf(ctx);
135+
DirectedGraph graph = Erdos.newGraphWithEngine(new AdjIncidenceGraphEngine(),
136+
Edge.EDGE_DIRECTION.DIRECTED,
137+
allow_self_loops, allow_multi_edges);
158138

159139
```
160140

161-
or use `PdfDocument.Builder`
141+
#### 3. algorithms introduction
142+
every algorithm extends `AbstractGraphAlgorithm<T, E extends IGraph>`, which is generically
143+
typed `E` for input graph and `T` for output and must implement
144+
* `T applyAlgorithm()` method
145+
146+
for example, the **`Bellman-Ford`** algorithm for single source shortest path,
147+
followed by the **`Floyd-Warshall`** algorithm for all pairs shortest paths.
148+
149+
```java
150+
private void BellmanFord()
151+
{
152+
SimpleDirectedGraph graph = new SimpleDirectedGraph();
153+
154+
Vertex s = new Vertex("s");
155+
Vertex t = new Vertex("t");
156+
Vertex x = new Vertex("x");
157+
Vertex y = new Vertex("y");
158+
Vertex z = new Vertex("z");
159+
160+
graph.addVertex(s);
161+
graph.addVertex(t);
162+
graph.addVertex(x);
163+
graph.addVertex(y);
164+
graph.addVertex(z);
165+
166+
graph.addEdge(s, t, 6);
167+
graph.addEdge(t, x, 5);
168+
graph.addEdge(x, t, -2);
169+
graph.addEdge(s, y, 7);
170+
graph.addEdge(y, z, 9);
171+
graph.addEdge(t, y, 8);
172+
graph.addEdge(z, x, 7);
173+
graph.addEdge(t, z, -4);
174+
graph.addEdge(y, x, -3);
175+
graph.addEdge(z, s, 2);
176+
177+
graph.setTag("graph");
178+
graph.print();
179+
180+
// apply the Bellman-Ford algorithm
181+
ShortestPathsTree res = new BellmanFordShortestPath(graph).setStartVertex(s).applyAlgorithm();
182+
// print it
183+
res.print();
184+
// apply the Floyd-Warshall algorithm
185+
AllPairsShortPathResult floyd_result = new FloydWarshall(graph).applyAlgorithm();
186+
// print the shortest paths tree of the vertex
187+
floyd_result.shortestPathsTreeOf(s).print();
188+
// print the shortest path between two nodes
189+
System.out.println(floyd_result.shortestPathBetween(s, z).toString());
190+
}
191+
192+
```
193+
194+
#### 4. algorithms, more examples
195+
this example shows the simplicity of the framework (hopefully ;)) where we apply 5
196+
different algorithms sequentally
197+
```java
198+
// perform a breadth first search
199+
BFS.BreadthFirstTree breadthFirstTree = new BFS(graph, s).applyAlgorithm();
200+
// perform a depth first search
201+
DFS.DepthFirstForest depthFirstForest = new DFS(graph).applyAlgorithm();
202+
// extract the strongly connected components of the graph
203+
ArrayList<HashSet<IVertex>> hashSets = new SCC(graph).applyAlgorithm();
204+
// perform a topological sort on the graph
205+
LinkedList<IVertex> res_sort = new TopologicalSort(graph).applyAlgorithm();
206+
// compute all pairs shortest paths using the Floyd-Warshall algorithm
207+
AllPairsShortPathResult floyd_result = new FloydWarshall(graph).applyAlgorithm();
208+
209+
```
210+
211+
#### 5. algorithms factories
212+
for major algorithms types, you can comfortably use the following algorithms factories
213+
* `MinSpanTreeFactory` - for Minimum Spanning Tree/Forest, for example:
214+
```java
215+
AbstractGraphAlgorithm<UndirectedGraph, IUndirectedGraph> alg = MinSpanTreeFactory.newMST(graph, MstAlgorithm.KRUSKAL, start_vertex);
216+
AbstractGraphAlgorithm<UndirectedGraph, IUndirectedGraph> alg2 = MinSpanTreeFactory.newMST(graph, MstAlgorithm.PRIM, start_vertex);
217+
```
218+
* `SingleSourceShortPathFactory` - for single source shortest path, for example:
219+
```java
220+
AbstractShortestPathAlgorithm alg = SingleSourceShortPathFactory.newSingleSourceShortPath(graph, SSSPAlgorithm.DIJKSTRA, start_vertex, end_vertex);
221+
```
222+
* `AllPairsShortPathFactory` - for shortest paths between all pairs, for example:
162223
```java
163-
new PdfDocument.Builder(ctx).addPage(page).filename("test").orientation(PdfDocument.A4_MODE.LANDSCAPE)
164-
.progressMessage(R.string.gen_pdf_file).progressTitle(R.string.gen_please_wait).renderWidth(2115).renderHeight(1500)
165-
.listener(new PdfDocument.Callback() {
166-
@Override
167-
public void onComplete(File file) {
168-
Log.i(PdfDocument.TAG_PDF_MY_XML, "Complete");
169-
}
170-
171-
@Override
172-
public void onError(Exception e) {
173-
Log.i(PdfDocument.TAG_PDF_MY_XML, "Error");
174-
}
175-
}).create().createPdf(this);
224+
AbstractGraphAlgorithm<AllPairsShortPathResult, IDirectedGraph> alg2 = AllPairsShortPathFactory.newAllPairsShortPath(graph, APSPAlgorithm.Johnson);```
176225
```
226+
#### 6. utilities
227+
a bunch of helper utilities can be found in the package **`com.hendrix.erdos.utils`**
228+
* **`SVertexUtils.java`** - query vertex order information inside a graph
229+
* **`SEdgeUtils.java`** - query edge order information inside a graph
230+
* **`SMatrixUtils.java`** - compute the adjacency and incidence matrix of a graph
231+
* **`SGraphUtils.java`** - get a sorted list of the weighted edges in a graph
177232

178233
### Contributions
179-
contributions are most welcomed, please consult [`CONTRIBUTING.MD`](CONTRIBUTING.MD)
234+
contributions are most welcomed, please consult [`CONTRIBUTING.md`](CONTRIBUTING.md)
180235

181236
### License
182237
If you like it -> star or share it with others
@@ -201,4 +256,4 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
201256
### Contact Author
202257
203258
* [Google+ TomershalevMan](https://plus.google.com/+TomershalevMan/about)
204-
* [Facebook - HendrixString](https://www.facebook.com/HendrixString)
259+
* [Facebook - HendrixString](https://www.facebook.com/HendrixString)

0 commit comments

Comments
 (0)