Represent a maze as binary matrix - matrix

I'm trying to apply Hillclimbing and Iterative deepening A* to solve this maze problem but i'm unable to represent this maze as binary matrix. How to represent this maze as binary matrix?
I'm trying to apply Hillclimbing and Iterative deepening A* to solve this maze problem but i'm unable to represent this maze as binary matrix. How to represent this maze as binary matrix?

Related

how to program Prim's and Kruskal's algorithm using adjacency lists in C

I have understood and implemented Prim's and Kruskal's algorithm using adjacency matrix but I am not understanding how to write a program using adjacency lists
I tried creating 2 matrices one for min weight for each edge and which is a two dimensional matrix and another matrix for the visited edges. But I couldn't proceed with that approach. Please provide an approach.
Creating a two-dimensional matrix from the adjacency lists is not needed. It would be the same as using adjacency matrix.
You should either sort a list of all edges by their weights (in Kruskal's algorithm), or use a heap to find a minimal vertex (in Prim's algorithm).

Which Graph Algorithms prefer adjacency matrix and why?

I heard that adjacency lists are used in most graph algorithms (but not all). I'm just wondering what algorithms prefer adjacency matrices and why?
So far I’ve found that Floyd Warshall uses adjacency matrices.
Adjacency lists are generally faster than adjacency matrices in algorithms in which the key operation performed per node is “iterate over all the nodes adjacent to this node.” That can be done in time O(deg(v)) time for an adjacency list, where deg(v) is the degree of node v, while it takes time Θ(n) in an adjacency matrix. Similarly, adjacency lists make it fast to iterate over all of the edges in a graph - it takes time O(m + n) to do so, compared with time Θ(n2) for adjacency matrices.
Some of the most-commonly-used graph algorithms (BFS, DFS, Dijkstra’s algorithm, A* search, Kruskal’s algorithm, Prim’s algorithm, Bellman-Ford, Karger’s algorithm, etc.) require fast iteration over all edges or the edges incident to particular nodes, so they work best with adjacency lists.
You mentioned that Floyd-Warshall uses adjacency matrices. While Floyd-Warshall does maintain an internal matrix tracking shortest paths seen so far, it doesn’t actually require the original graph to be an adjacency matrix. The overall cost of the dynamic programming work is Θ(n3), which is bigger than the O(n2) cost of converting an adjacency list into an adjacency matrix or vice-versa.
There are only a few places where an adjacency matrix is faster than an adjacency list. Adjacency matrices take time O(1) to test whether a particular edge is present in the graph, which is faster than the O(deg(v)) cost of the corresponding operation on an adjacency list. Since the cost of converting an adjacency list to an adjacency matrix is Θ(n2), the only cases where an adjacency matrix would outperform an adjacency list are in situations where (1) random access of the edges are required and (2) the total runtime of the algorithm is o(n2). I only know a few algorithms that do this. For example, there’s the celebrity-finding problem where you’re given a graph and are asked to find whether there’s a node with incoming edges from each node and outgoing edges to no nodes. This can be done in time O(n) using an adjacency matrix, faster than what can be done with an adjacency list.
(That being said, you could also use an adjacency list represented using cuckoo hash tables rather than regular lists and match the same runtime bounds as above, though with the cost of creating the adjacency list now only expected to be fast rather than actually worst-case efficient.)
The main reason I’ve found adjacency matrices to be useful is in thinking about graphs from a different perspective. For example, raising an adjacency matrix to the kth power makes a new matrix that counts the number of paths from one node to another using exactly k hops. This can be used to count and find triangles in graphs faster than the naive algorithm, for example. Similarly, the Four Russians algorithm for computing transitive closures of graphs works by representing the graph as a matrix and using some clever techniques (treating blocks of bits as integers then used in a lookup table) to outperform the naive search.
Hope this helps!

Is there any efficient algorithm to find the length of the longest cycle in a undirected graph?

I want to know that is there any efficient algorithm to find the length of the longest cycle in a graph?
The graph is an undirected graph.
The algorithm doesn't have to tell what vertex is in the cycle, just only the length.
The problem of finding the longest cycle in a graph is NP-hard, because solving this problem allows to answer the question "Is this graph hamiltonian ?" (does it possess an hamiltonian cycle), which is itself a NP-complete problem.
So, indeed, no efficient algorithm can do that.
There are methods based on matrix multiplication to find a cycle of length k in a graph.
You can find explanations about finding cycles using matrix multiplication in this quesion. But beware, the matrix multiplication methods allows to detect walks of a given length between 2 vertices, and the repetition of vertices is allowed in a walk.

Algorithm to connect all dots with the minimum total distance

I have a set of points and a distance function applicable to each pair of points. I would like to connect ALL the points together, with the minimum total distance. Do you know about an existing algorithm I could use for that ?
Each point can be linked to several points, so this is not the usual "salesman itinerary" problem :)
Thanks !
What you want is a Minimum spanning tree.
The two most common algorithms to generate one are:
Prim's algorithm
Kruskal's algorithm
As others have said, the minimum spanning tree (MST) will allow you to form a minimum distance sub-graph that connects all of your points.
You will first need to form a graph for your data set though. To efficiently form an undirected graph you could compute the Delaunay triangulation of your point set. The conversion from the triangulation to the graph is then fairly literal - any edge in the triangulation is also an edge in the graph, weighted by the length of the triangulation edge.
There are efficient algorithms for both the MST (Prim's/Kruskal's O(E*log(V))) and Delaunay triangulation (Divide and Conquer O(V*log(V))) phases, so efficient overall approaches are possible.
Hope this helps.
The algorithm you are looking for is called minimum spanning tree. It's useful to find the minimum cost for a water, telephone or electricity grid. There is Prim's algorithm or Kruskal algorithm. IMO Prim's algorithm is a bit easier to understand.
here http://en.wikipedia.org/wiki/Minimum_spanning_tree you can find more information about the minimum spanning tree, so you can adapt it to solve your problem.
Take a look at the Dijkstra's algorithm:
Dijkstra's algorithm, conceived by Dutch computer scientist Edsger Dijkstra in 1956 and published in 1959, is a graph search algorithm that solves the single-source shortest path problem for a graph with nonnegative edge path costs, producing a shortest path tree. This algorithm is often used in routing and as a subroutine in other graph algorithms.
http://en.wikipedia.org/wiki/Dijkstra's_algorithm

General purpose algorithm for triangulating an undirected graph?

I am playing around with implementing a junction tree algorithm for belief propagation on a Bayesian Network. I'm struggling a bit with triangulating the graph so the junction trees can be formed.
I understand that finding the optimal triangulation is NP-complete, but can you point me to a general purpose algorithm that results in a 'good enough' triangulation for relatively simple Bayesian Networks?
This is a learning exercise (hobby, not homework), so I don't care much about space/time complexity as long as the algorithm results in a triangulated graph given any undirected graph. Ultimately, I'm trying to understand how exact inference algorithms work before I even try doing any sort of approximation.
I'm tinkering in Python using NetworkX, but any pseudo-code description of such an algorithm using typical graph traversal terminology would be valuable.
Thanks!
If Xi is a possible variable (node) to be deleted then,
S(i) will be the size of the clique created by deleting this variable
C(i) will be the sum of the size of the cliques of the subgraph given by Xi and its adjacent nodes
Heuristic:
In each case select a variable Xi among the set of possible variables to be deleted with minimal S(i)/C(i)
Reference: Heuristic Algorithms for the Triangulation of Graphs

Resources