OpenGL ES 1.0 question about vertex shader - opengl-es-1.1

Is there a way to set a variable with value 0 only in the first vertex, and each vertex is read, it add 1 ? .
And this variable that equals to 1, in the second vertex, don't go back to value 0, and continues to add 1 each vertex .
Like this:
vertex N°0: var a=0;
vertex N°1: var a=1;
vertex N°2: var a=2;
(And so on)

Related

An example of connected components in an undirected graph

I am doing an algorithm problem about finding connected components in an undirected graph.
The input is a list of edges stores in (source target), and the output is (vertex,label).
source is an integer representing id of source vertex,
target is an integer representing id of target vertex,
vertex is vertex id,
label is the label of a connected component to which vertex belongs.
Here are two examples:
Example 1.
input:
0 1
1 2
3 1
output:
(0, 0)
(1, 0)
(2, 0)
Example 2.
input:
0 3
4 5
output:
(3, 0)
(4, 4)
(5, 4)
In my understanding, for the 1st example, all vertex connect to 1, so the graph has one connected components, so the result should be
(0,0)
(1,0)
(2,0)
(3,0)
but the answer does not have (3,0).
For the 2nd example, 0 connect to 3, 4 connect to 5, there are two connected components. The result should be
(0,0)
(3,0)
(4,4)
(5,4)
but the answer does not have (0,0)
I am not sure do I misunderstanding something that the result is unequal to the output it gives..
Come answer my silly question again... The main point is those two input is a same graph. And the label should be the smallest vertex in the component.

Creating a Map Reduce Function in Python from edge list

I need some help creating a Map Reduce function in Python from an edge list.
Given the following list:
A,B
A,C
A,D
B,C
C,A
C,B
D,A
My code should follow the format below. My goal is to display a degree list along with the count of degrees.
map(key, value):
//key: document name; value: text of the document
for each word w in value:
emit(w, 1)
reduce(key, values):
//key: a word; value: an iterator over counts
result = 0
for each count v in values:
result += v
emit(key, result)
Loops have always been a struggle for me. Can someone point me in the right direction?
The output should be as follows:
Degree Count
1 2
2 1
3 1
The code should work for any data set similarly formatted
So, as I understand it, I need to count the number of different values each letter is paired with--this would be the "degree" (A, for example, is degree 3), and then total the pairs according to the degree--this would be the "count".
Could the key be the pair of values?
map(key, value):
//key: document name; value: text of the document
for each word w in value:
emit([x,y], 1)
I'm not sure if MapReduce is the best way to approach this problem, but I think the following makes sense.
First Map each Vertex-Edge pair to the (Vertex, 1). Then Reduce by summing the counts for each Vertex.
map(key, value):
//key: vertex; value: edge
emit(key, 1)
reduce(key, values):
//key: vertex; value: an iterator over counts
result = 0
for each count v in values:
result += v
emit(key, result)
This assumes that the input rows are unique.

Finding an edge by unique ID or name

It is possible to search a subgraph and node by their unique names;
n = agnode(g, "myUniqueNodeName", FALSE);
h = agsubg(g, "myUniqueSubgrahName", FALSE);
Likewise, is there a way to search edges in a strict directed graph by their unique names?
e = agedge (g, u, v, "e28", FALSE);
Documentation indicates that:
The 'name' of an edge (more correctly, identifier) is treated as a
unique indentifier for edges between a particular node pair. That is,
there can only be at most one edge with name e28 between any given u
and v, but there can be many other edges between other nodes.
It seems that there must be a list of edges that can be searched by name. Otherwise, a separate (ID -> edge) map will need to be maintained separately.

Flex and polygonal.de graph classes in pathfinding?

I am wondering if someone have done this already, to send me into right direction..
The issue is as follow : I have a 2 dimensional array, on which i hold integer numbers, if the number is 0 - the item shall not be included into the graph, if it is 1 - it must be included.
The result graph shall be used for patfinding ( the shortest path ) to some element.
how to turn this 2 dimensional array into the graph ? ( with polygona.de classes if possible ),
I am currently trying with Polygonal.de classes. any suggestions and points into the right direction is more than appreciated.
This is the 2 dimensional structure. The red cells is prohibited to walk on, and there must be found optimal path from "start" to the "end". But 1st things 1st - i need to turn this 2 dimensional structure into a graph now :|
The way I see it, your 2D array is already a graph. A node of the graph is represented by a pair (i, j) and may have neighbor nodes such as (i + 1, j), (i, j + 1), etc. You can write a utility function for your array that hides these low-level neighbor definitions and skips the cells that are occupied.
The de.polygonal.ds API for the Graph data structure contains this example for the construction of a graph:
var graph = new de.polygonal.ds.Graph<String>();
var a = graph.addNode("a");
var b = graph.addNode("b");
var c = graph.addNode("c");
graph.addSingleArc(a, b, 1.0);
graph.addSingleArc(b, a, 1.0);
graph.addMutualArc(a, c, 1.0);
Adjust the example to construct a 2D array that contains a node for each free (i, j) of the original 2D array. Then traverse the 2D array of nodes and call addMutualArc() to connect adjacent nodes.

Dijkstra's algorithm to find all the shortest paths possible

I'm working on Dijkstra's algorithm, and I really need to find all the possible shortest paths, not just one. I'm using an adjacency matrix and I applied Dijkstra's algorithm, and I can find the shortest path. But I need to find all the paths with that minimum cost, I mean all the possible solutions, if they exist.
This is how my algorithm works, for a single solution:
public void dijkstra( int graph[][] )
{
int d[] = new int[ graph.length ];
int dC[] = new int[ graph.length ];
int p[] = new int[ graph.length ];
for( int i = 0; i < graph.length; i++ ){
d[ i ] = 100; dC[ i ] = 100; p[ i ] = -1;
}
d[ 0 ] = 0; dC[ 0 ] = 0;
int i = 0, min = 200, pos = 0; //You can change the min to 1000 to make it the largest number
while( i < graph.length ){
//extract minimum
for( int j = 0; j < dC.length; j++ ){
if( min > d[ j ] && dC[ j ] != -1 ){
min = d[ j ]; pos = j;
}
}
dC[ pos ] = -1;
//relax
for( int j = 0; j < graph.length; j++ ){
if( d[ j ] > graph[ pos ][ j ] + d[ pos ] ){
d[ j ] = graph[ pos ][ j ] + d[ pos ];
p[ j ] = pos;
}
}
i++; min = 200;
}
for( int j = 0; j < p.length; j++ ){
System.out.print( p[ j ] + " " );
}
System.out.print( "\n" );
for( int j = 0; j < d.length; j++ ){
System.out.print( d[ j ] + " " );
}
System.out.print( "\n" );
}
If you look at Dijkstra's algorithm in pseudocode form here:
Wikipedia Dijkstra's Algorithm Pseudocode
You will notice the line referred to as a Relax. Right now it only contains a case for if the path found is less than the current shortest path, but there isn't anything done if they are equal. You should probably keep all the equally short paths in a List.
If your implementation of Dijkstra's algorithm is based on a priority queue, take your first solution, record the depth and keep popping solutions out until the distance changes.
If your graphs allows edges with weight = 0 and also allows cycles, bear in mind that there are infinitely many shortest paths, and you cannot hope to output them all!
If there are either no zero-weight edges, or your graph is a DAG, then your solution is still problematic: it either doesn't produce all shortest paths as required, or it does so withO(2^N) space and time complexity. But then, perhaps there might be as many shortest paths, so you can not hope to do better in the general case.
This is the best source for a slightly more general problem: Finding the k Shortest Paths (Eppstein). You could apply this by iterating the shortest paths, stopping at the first path that is longer than the previous.
For the restricted problem of finding only the (equivalent) shortest paths, Anderson Imes' hint above (is this homework? Otherwise someone should spell it out) is good and much simpler than the above.
I'm not very sure Dijkstra's algorithm can be easily adapted to do that; of course is not as simple as removing the visited edges, because the n shortest might share some of them.
So an almost brute force, but heuristically oriented solution, is to try this:
for every edge E in the shortest path:
remove E and run the modified Dijkstra again over the new graph
if the shortest path is longer than the first one obtained, stop
else, repeat recursively removing one edge at a time from the new sub-graph
My intuition tells me that it should work, with an increase in complexity proportional to the length (n in number of edges) of the first solution... If there are no more shortest path, it should end in the first round, if it founds another, it goes on with n-1 tries. The worst case complexity increase estimation over the original algorithm is very bad (n! I guess?), but that also means that there are lots of paths, so that is a work that needs to be done with any algorithm...
edit: Thinking a little bit more, the complexity increase might be even larger than the factorial of the number of nodes of the first found path, as the second might have even more nodes! So I think it's very difficult to estimate the complexity of this method, but it should be something like the number of valid solutions times the average number of nodes in the paths times the number of nodes squared (this last term being the original complexity of the unmodified algorithm).
edit 2: I've found a research paper about this subject, it requires subscription to get the full text but maybe you can find it somewhere else: http://ieeexplore.ieee.org/Xplore/login.jsp?reload=true&url=http%3A%2F%2Fieeexplore.ieee.org%2Fiel5%2F7719%2F21161%2F00982778.pdf%3Farnumber%3D982778&authDecision=-201
FloydWarshallShortestPaths class of JgraphT finds all shortest paths. Based on above comments, you are looking for shortest paths between two vertices. You may want to use getShortestPaths method, which will return all shortest paths from the a vertex to all other vertices. Then you may want to filter the result which interests you.
Consider this algorithm: https://www.programmingalgorithms.com/algorithm/dijkstra's-algorithm/php/
At the end there is a "< $distance[$v])" condition. If we change it to "<= ...", it will give all the existing shortest paths.
For collecting the paths we can put a
$this->paths[$v][] = $u;
row here also.
The whole snippet is here: https://pastebin.com/gk2ymyWw
if (!$this->known[$v] && $graph[$u][$v] && $this->distance[$u] != $this->INT_MAX && $this->distance[$u] + $graph[$u][$v] <= $this->distance[$v]) {
$this->distance[$v] = $this->distance[$u] + $graph[$u][$v];
$this->paths[$v][] = $u;
}
This paper from 1982 describes an algorithm for graphs with multi-dimensional edge weights, that gives all shortest paths.
The algorithm works fine with simple weighted graphs, so should work for your case.
The author compares it to Dijkstra, both in how it works and in a run-time complexity comparison.
In pseudocode, paraphrasing the paper:
1. H is a heap of paths sorted on the weights of these paths, either
a. lexicographically and decreasing in each element of the weight vector
b. on the negative sum of all the elements in the weight vector
Initially, H contains only path p0 (the starting point) the weight of which is O.
2. S1 through Sv are sets associated with vertices v1 through vv respectively.
These sets contain the maximal paths to each vertex, as they are discovered.
Initially, all sets are empty.
3. a. While (H is not empty) do begin
b. remove the root of H, p;
c. if p is not dominated by any of the paths in Sn where vn is last vertex of p
d. add it to Sn (the set of maxima for vertex vn)
e. for each possible extensions q of path p
g. if path q to node vm is not yet dominated by the maxima in Sm
h. insert q into H
I Just solved a task to find all possible shortest paths on leetcode.com using Dijkstra's algorithm.
The only difference is how you extract information from "parents" and "distances" arrays.
The main idea is
you are moving from target to source and from each node in your optimal path ("parents" array),
you are looking for neighbors who had same minimal distances to the source like recorded parent, and
then recursively moving through all of those possible parents till you reach the source.
Below is the code in Python.
if parent[endNode] > -1: # -1 means no path at all
self.traversingAllNodes(parents, shortestDistances, endNode, [endNode])
return self.finalList
def traversingAllNodes(self, parents, distances, startNode, path):
if parents[startNode] == -1:
self.saveFound(path) # saving one path
return
currentNode = startNode
parent = parents[currentNode] # recorded parent
optimalDistance = distances[parent] # distance from parent to source
for node in self.graph[currentNode]: # moving via neighbords
if distances[node] == optimalDistance:
path.append(node)
self.traversingAllNodes(parents, distances, node, path)
path.remove(node)

Resources