Data Structure-graph Storage Structure Representation and traversal (DFS amp; BFS)
1. Graph structure represented by the adjacent matrix
/* Graph structure represented by the adjacent matrix */# include
# Include
# Include
# Include
# Include
# Include
Usi
BFS is another important algorithm of graphs, the following is the BFS algorithm based on adjacency table representation.
/* Graph adjacency table representing BFS input:1 7 A 1 5 B 2 4 3 C 2 4 2 D 3 6 5 2 E 3 7 4 1 F 1 4 G 1 5 output:a E D g B F C * * * #include
created for each vertex vi in the graph, and the vertices adjacent to the Fish VI are placed in the linked list.Adjacency Matrix:An adjacency matrix is a matrix that represents the neighboring relationships between vertices. Set g (V,e) is an n-vertex-free graph, then it corresponds to an n-order matrix. If the square is set to a, then for A[I][J] There are two kinds of values, when a[i][j]=1, the vertex i
numbered 2i+1 is the right child node;
Small Experiment (C implementation)Here we first use an array to generate a completely binary tree (chained storage), and then deep search using the pre-sequence traversal, wide search with their own implementation of a queue (chain storage) to do. The figure is as follows:
The code is:#include "Article Address is: Http://blog.csdn.net/thisinnocence" Algorithm Learning notes (vi) bina
Topic Link: hdu_5876_sparse GraphAttach the fork sister's puzzle:1009 Sparse Graph [by Ftiasch]Test instructions: The N-point of the non-full graph removes the M-bar, asking the point s to the shortest length of the other points.ExercisesThe BFS on the complement map is a very classic problem. The general practice is to use a linked list (or lazy with std::set) t
;Visit=new Boolean[vernum];distance=new int [vernum];endt=new int [size];startt=new int [size];Constructs vertices to prevent vertices in the following forward graph from being arcs, so that the vertex array is a null pointerfor (int i=0;i{Adj[i]=new Vertex (i, 0, NULL);}}/*** Create diagram Insert all nodes to create Diagram connection table representation* x y indicates arc head, and ARC tail respectively*/public void Inserte (int x,int y){Vertex Te
The breadth-first traversal of the Ten algorithms:In this paper, we describe the implementation of the breadth-first traversal algorithm of graph based on Java, and the detailed methods are as follows:Using adjacency matrix to store graph method:1. Determine the number of vertices and edges of the graph2. Input vertex information is stored in one-dimensional array vet3. Initialize the adjacency matrix.4. En
) { intQ =G[p][i]; if(S1.find (q) = = S1.end ())Continue;///Determine If the Q point has determined the distance;s1.erase (q); S2.insert (q); } Setint>:: Iterator it; for(It=s1.begin (); It!=s1.end (); it++)///The points that cannot be reached can be reached by P-point;{dist[*it] = Dist[p] +1; Q.push (*it); } s1.swap (s2);///exchange of S2 and S1;s2.clear (); }}intMain () {intT; scanf ("%d", T); while(t--) { intN, m, start; scanf ("%d%d", n, m);
Hdu 1689 Alien's neck.pdf (bfs layered graph pruning)
Alien's neck.pdf
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission (s): 1526 Accepted Submission (s): 415
Problem DescriptionJYY is taking a trip to Mars. to get accepted by the Martians, he decided to make a magic neck.pdf for their king. (Otherwise, JYY will be eaten) Now, he has collected using magic bi
The question says that, starting from S, we can split up and forward in S or. After thinking about it, we find that we need to find a minimum spanning tree.
First, bfs pre-processes the distance between each two points. My program uses map to map the coordinates of each point to 1-N, which makes it easier to create a graph.
Then, the prime is enough. Note that when you use gets () to read a map, you must us
Theory:
The depth_fisrst search traversal is similar to the first root traversal of a tree. It is a promotion of the first root traversal of a tree:
The breadth_first search traversal process is similar to the hierarchy traversal process of the tree:
JAVA Implementation
Vertex. Java
Package diagram; public class vertex {string value; Boolean isvisited; vertex (string value) {This. value = value; this. isvisited = false;} Public String getvalue () {return value;} public void setvalue (string
the search together. that is, if the original group walks five steps, then splits into two groups each walking three steps, the total distance is 11 = 5 + 3 + 3.
InputOn the first line of input there is one integer, n OutputFor every test case, output one line containing the minimal cost of a succesful search of the Maze leaving no aliens alive.
Sample Input
26 5##### #A#A### # A##S ####### 7 7##### #AAA#### A## S #### ##AAA########
Sample output
811
Given a maze, create a minimal
1 0 10 0 1 11 0 0 10 0 1 11 1 1 01 0 1 11 1 1 10 0 1 00 1 1 0Sample output
6
Question: RT
Idea: BFS brute-force search
Pay attention to the following points:
1. Each time the scaling status is expanded, right-handed to 9 blocks
2. There may be many endpoints, because some grids still have the same pattern after rotation.
3. pay attention to the details during processing (this has pitted me for a long time. For example, when I rotate 3rd blocks, I als
These algorithms are mainly useful in the future, so we will give them here.
1) The structure definition of the graph (similar to the previous one, just to facilitate the algorithm, here we add:
///
BFS algorithm and path printing:
///
not node in self.visited: queue.append(node) order.append(node) bfs() print order return orderif __name__ == '__main__': g = Graph()g.add_nodes([i+1 for i in range(8)])g.add_edge((1, 2))g.add_edge((1, 3))g.add_edge((2, 4))g.add_edge((2, 5))g.add_edge((4, 8))g.add_edge((5, 8))g.add_edge((3, 6))g.add_edge((3, 7))g.add_edge((6, 7))print "nodes:", g.nodes()order = g.breadth_first_se
:") forPinchAdjacency_map:Print(P,":", Adjacency_map[p])Print ()Print(Adjacency_map)Print (5*"hello! ")#4Doing DFS, which is equivalent to a tree's pre-order traversal, is quite concise in Python writing. DFS is a node that encounters deeper depths to execute immediately, rather than dragging along like BFS, joining the queue until it is impossible to traverse the same layer to select depth (sequence traver
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.