java,algorithm,graph,breadth-first-search,adjacency-matrix

You hit the condition temp == end at the first iteration because your temp starts from 0, then it's 1 without inserting element to queue(because it's not adjacent to 0), then it's 2 without inserting, and here you are, under condition temp == end (both equal 2) you return Q...

Your format specifier "The answer is %d""\n" is for one argument, but you pass two, path(arr, u, v) and ".\n": printf("The answer is %d" "\n", path(arr, u, v),".\n"); Presumably you need printf("The answer is %d.\n", path(arr, u, v)); ...

java,algorithm,cycle,adjacency-matrix

There is two good solutions: begin to traverse (bfs, dfs , ...) your graph if you visited a node twice there is cycle in your graph. hence you have a adjacency matrix, then you could use algorithm that Imran mentioned in comment, you just need to compute An, for n...

java,arrays,algorithm,graph-theory,adjacency-matrix

Try this: static void set(boolean[][] aM, int cols, int row0, int col0, int row1, int col1) { int index0 = row0 * cols + col0; int index1 = row1 * cols + col1; aM[index0][index1] = aM[index1][index0] = true; } static boolean[][] adjacencyMatrix(char[][] cA) { int rows = cA.length; int cols...

c++,multidimensional-array,dijkstra,shortest-path,adjacency-matrix

Converting an image into an adjacency matrix is completely unnecessary; the only thing you need is the "neighbor-ness" information which is already implicitly present. The neighbors of any pixel i,j are represented by the pixels at (i-1,j), (i+1,j), (i,j-1), (i,j+1) subject to image boundaries. You don't need a matrix to...

r,gis,spatial,adjacency-matrix

The matrix us2.adj is symmetric. The problem is with the test. It turns out that isSymmetric(us2.adj) uses all.equal(...) to test for equality of the matrix with it's transpose, and all.equal(...) checks the attributes as well as the values. nb2mat(...) creates a matrix with the row names set to the polygon...

There are several major problems with this code, here's a few: The threads are created, but never started. This means the code they contain will never run. For that to occur, the start() method must be called on each thread that is created. The threads are not "joined" at the...

java,algorithm,search,graph-theory,adjacency-matrix

You don't extract the shortest path correctly. You are adding every node you process to the shortest path. If you want to use BFS to find the shortest path, you have to keep track of the shortest path to every intermediate node by storing "back edges" and only in the...

Matrix multiplication should work here set.seed(1) dat <- data.frame(id=paste0("r", 1:10), A=sample(0:1,10,TRUE), B=sample(0:1,10,TRUE), C=sample(0:1,10,TRUE)) mat <- as.matrix(dat[-1]) t(mat) %*% mat EDIT Or in a one-liner (thanks @AnandaMahto) crossprod(as.matrix(dat[-1])) ...

algorithm,matlab,adjacency-matrix,clique-problem

I don't know whether there is a better alternative which should give you direct results, but here is one approach which may serve your purpose. Your input: >> A A = 0 1 1 0 1 1 0 0 1 0 0 1 1 0 1 1 0 0 1...

matlab,sparse-matrix,sparse,adjacency-matrix

Use the spdiags function to convert the degree vector to a sparse diagonal matrix. Then subtract the adjacency matrix from diagonal matrix to get the Laplacian. Example using your code: adj = spconvert(adj); for i=1:size(adj, 1) degree(i) = CalcDegree(adj, i) end D = spdiags(degree, 0, size(adj, 1), size(adj, 2)); L...

c++,function,recursion,adjacency-matrix,undirected-graph

As Linxi's comment says, you are copying the seen vector on every invocation of your recursive function so that the value returned back to your main function has exactly one nonzero entry at index i. Pass a reference instead (I've also passed the matrix as reference to const to prevent...

matlab,matrix,octave,graph-theory,adjacency-matrix

One way would be to write your own algorithm using some sort of electrostatic repulsion as in the paper you linked. Can probably be done in less than 40 lines of Matlab (it seems others have tried). But sometimes, it is better to use external tools than to do everything...

python,arrays,matrix,adjacency-matrix

Use the function scipy.spatial.distance.cdist(): import numpy as np def array_to_complete_graph(vertexarray): vertcoords = np.transpose(np.where(vertexarray == True)) cg_array = np.eye(len(vertcoords)) for idx, vals in enumerate(vertcoords): x_val_1, y_val_1 = vals for jdx, wals in enumerate(vertcoords): x_diff = wals[0] - vals[0] y_diff = wals[1] - vals[1] cg_array[idx,jdx] = np.sqrt(x_diff**2 + y_diff**2) return cg_array arr...

python,arrays,list,adjacency-matrix

The * operator is not always a good thing for creating lists, specially multi-dimensional lists. This operator just creates copies of the elements in the list to populate the larger list. This works fine for primitive types, but not for other objects. For example, when you write [0] * 100,...

java,algorithm,recursion,adjacency-matrix,bipartite

You need to replace findMatch with something like this: public static boolean findMatch(char [][]M, int myRow) { if(myRow < 0) return true; for(int c = 0; c < M.length; c++) { if(M[myRow][c] == 'y' && !isTaken(M, myRow, c)) { M[myRow][c] = 't'; if (findMatch(M, myRow-1)) return true; M[myRow][c] = 'y';...

Does the following do what you are looking for? # simulate data. d <- data.frame( id=c(2,3,4,5,6,6,8,11,11,12,12), author=c("FN", "VM","VA","FK","VM","SM","FK","FK","VB","FK","VB") ) d id author 1 2 FN 2 3 VM 3 4 VA 4 5 FK 5 6 VM 6 6 SM 7 8 FK 8 11 FK 9 11 VB 10...

matlab,graph,3d,adjacency-matrix

This can be used for arbitrary dimensional simplicial data: halfedges = nchoosek(1:size(Faces,2), 2); edges = [halfedges; fliplr(halfedges)]; A = logical(sparse(Faces(:,edges(:,1)), Faces(:,edges(:,2)), 1)); If memory overhead is more of a concern to you than speed, you could loop over the edges: nv = max(Faces(:)); % Get the maximum vertex id. halfedges...

You have a number of issues where you attempt to read beyond the end of arr resulting in undefined behavior. First when you declare an array with N = 11 elements, your array indexes are arr[0] - arr[10]. Consider the following in path: return path2 (A, N, v, u); You...

c,graph,segmentation-fault,edge,adjacency-matrix

Your initialization function is initializing the matrix incorrectly: g->mat = calloc(max, sizeof(double*)); for (i = 0; i < max; i++) g->mat = calloc(max, sizeof(double)); Note that each iteration of the loop is recording the resulting pointer in the same (wrong) place: g->mat. The quickest way to fix it would be...

java,data-structures,directed-acyclic-graphs,adjacency-matrix

There are many options: You could add a pointer back to the header to the ListNode class, turning it into a full edge representation. You could change your representation to have dedicated Vertex and Edge classes. You could return a node pair (e.g. an ListNode array of size 2) containing...

arrays,scala,graph,adjacency-matrix

I allowed myself to refactor your code a bit: class Graph(val vertex: IndexedSeq[String], edges: Seq[(Int, Int)]) { def size: Int = vertex.length val index: Map[String, Int] = vertex.zipWithIndex.toMap val adjacent = edges groupBy (_._1) mapValues (_ map (_._2)) def adjacencyMatrix = adjacent mapValues (_.toSet) mapValues (0 to size map _)...

Perhaps this is what you want: A = zeros(numel(L)); A(logical(L),logical(L)) = 1; The result in your example is A = 1 1 1 0 0 1 1 1 1 1 0 0 1 1 1 1 1 0 0 1 1 0 0 0 0 0 0 0 0 0...

r,matrix,binary,igraph,adjacency-matrix

Here's an approach that gets you your desired output: tcrossprod(table(mydf)) # user_id # user_id 98461 311542 875740 24376261 # 98461 3 0 1 0 # 311542 0 1 0 0 # 875740 1 0 1 0 # 24376261 0 0 0 1 (tcrossprod(table(mydf)) != 0) + 0 # user_id #...