[C ++] LeetCode: 129 Clone Graph (deep copy BFS amp; DFS of a Graph)
Question:
Clone an undirected graph. Each node in the graph containslabelAnd a list of itsneighbors.
OJ's undirected graph serialization:
Nodes are labeled uniq
(front//queue is not empty when looping{front++;W=qu[front].data; //out of the team vertex Wif (w==v)//The inverse of the output path when V is found and exits{I=front; //Output reverse path via queuewhile (Qu[i].parent!=-1){printf ("%2d", qu[i].data);I=qu[i].parent;}printf ("%2d\n", qu[i].data);Break}p=g->adjlist[w].firstarc; //Find First adjacency point of Wwhile (P!=null){if (visited[p->adjvex]==0){visited[p->adjvex]=1;rear++; //Put the non-visited adjacency points of W into the teamqu[r
2) DFSThe depth-first search always searches for the starting edge of the recently discovered node v until all the departure edges of that node are discoveredOnce all the starting edges of node v are found, the search is traced back to the V's precursor node.Implementation Details: TimestampEach node has a discovery time and a completion time.The precursor of a DFS rear image forms a depth-first forest1#include 2 using namespacestd;3 #defineNIL-14 intg[ +][ +];5 intN;6 structNode7 {8 intcolo
Easy Graph problem?Topic Abstraction: Give you a matrix of n * M. Each lattice is a number or an obstacle ' * '. First question: The shortest path from the starting point to the end point. Second question: Require the same direction can not walk two times, that is, each to arrive at a lattice, need to change direction. Ask the shortest circuit. But there is no output-1.Analysis: Can be directly BFS search.
1) BFSBreadth-First search: Given source node s, generate breadth-first search treeThe simple path from node s to node V in the breadth-first search tree corresponds to the shortest path from S to V (the path with the least number of edges)Breadth first: The boundary between the discovered node and the uncovered node (the gray node) expands outwards along its breadth1#include 2#include 3#include 4#include 5 6 using namespacestd;7 #defineINF 999999998 #defineNIL-19 intN;Ten intg[ +][ +]; One stru
{front++;W=qu[front].data; //out of the team vertex Wif (w==v)//The inverse of the output path when V is found and exits{I=front; //Output reverse path via queuewhile (Qu[i].parent!=-1){printf ("%2d", qu[i].data);I=qu[i].parent;}printf ("%2d\n", qu[i].data);Break}p=g->adjlist[w].firstarc; //Find First adjacency point of Wwhile (P!=null){if (visited[p->adjvex]==0){visited[p->adjvex]=1;rear++; //Add the inaccessible adjacency points of W into the teamqu[rear].data=p->adjvex;Qu[rear].parent=fro
do I track what I've visited?HashSet:: Constants Add, delete, and searchHow do I track the path from the start to the target?HASHMAP: Link Each node to the node that discovers itStack and recursive implementation process (pseudo code):BFS (breadth-first search) algorithmBreadth-first-searchBFS is the node that traverses the tree, starting at the root node, along the width of the tree.If all nodes are accessed, the algorithm aborts. Implementation of
Breadth-First traversalBreadth-first traversal is a very common and common way to traverse a graph, except for BFS and DFS, which is the depth-first traversal method, which I will write in my next blog post.Traversal processI believe that every person who read this blog can read the adjacency list storage diagram.Do not understand the person, please first learn the storage method. In my previous blog.Portal
G.adjacentedges (v) Do4 ifVertex W isn't labeled as discovered then5Recursively call DFS (G,W)The DFS pseudo-code for non-recursive notation is as follows:Input G v of G1Procedure Dfs-iterative (G,V):2Let S be a stack3S.push (v)4 whileS is not empty5V←s.pop ()6ifV is not labeled as discovered:7Label V as discovered8 forAll edges from V to W in G.adjacentedges (v) Do9S.push (W)BFS (Breadth-first Search)This is the relative to the
The graph of data structure Experiment five: The shortest steps from the starting point to the target point (BFS)Time limit:1000ms Memory limit:65536kb
problem Description
In the ancient Legend of Warcraft, there are two legions, one called the Scourge, and the other called Janissary. In their area, there are n passes, numbered 1. N, there is a channel connection between some passes. Among them, the Legion
[LeetCode from zero to single row] No133. clon graph (BFS breadth-first search), leetcodebfsBackground (the following background information reproduced from: http://www.cnblogs.com/springfor/p/3874591.html? Utm_source = tuicool)
DFS (Dpeth-first Search)As its name implies, it is a deep search, where one path goes to the dark and another path is selected.I remember when I went to Algorithm, the professor gav
Huadian North Wind BlowsKey laboratory of cognitive computing and application, Tianjin UniversityLast Modification Date: 2015/8/22There are adjacency matrices, adjacency linked lists, sparse matrices and so on for the storage of the graph.The non-direction graph mainly includes the two sides content, the graph traverse and seeks the unicom component.One, the traversal of the
Test instructions: give you n point M edge to form an no-map, ask you to find the point of the map on the complement to each point distance of the minimum value, each edge distance of 1Complement map: Complete graph minus OriginalFull picture: Every two points connected to the graphIn fact, is a skilled bfs, we can see that although a lot of points but few sides, the use of vectors to save each point in the
Breadth-First traversalBreadth-first traversal is a graph traversal method that is often seen and common, with the exception of BFS and DFS, which is the depth-first traversal method. I'll write it in my next blog post.Traversal processI believe every person who looks at this blog will be able to read and understand the adjacency list storage diagram.Do not understand the people. Please learn the storage me
is considered to be index, delete } } returncount==N; }}2.DFSThe pre must be recorded in DFS to avoid a->b->a such traversal, which is considered a loop.classSolution { Public BooleanValidtree (intNint[] edges) { if(n==0)return false; List[] List=NewList[n]; for(inti=0;i) {List[i]=NewArraylist(); } for(int[] pair:edges) {list[pair[0]].add (pair[1]); list[pair[1]].add (pair[0]); } Boolean[] visited=New Boolean[n]; if(Dfs (list, visited, 0,-1))retur
| Help center | welcome, liang530, office call: 0738-8371676
HomepageYou are welcome to visit the Program Design Competition website of Hunan Institute of humanities and technology. We are striving to do better!
System Homepage| Help |Full-text search
Discussion board| Forum list
Question list| Upload questions| Submission status
The competition is over|Reserve a competition |Current competition
User ranking| Modify personal information| User Registration
Data Structure: Figure 2 (
expansion node from the unsed and joins the temporary auxiliary collection $ if(Unsed.find (edge[k].to)! =Unsed.end ()) {Panax Notoginseng unsed.erase (edge[k].to); - Hep.insert (edge[k].to); the } + } A for(Setint>::iterator it = Unsed.begin (); It! = Unsed.end (); it++) {//Unsed temporarily preserves points that are not adjacent to the current expansion node theQu.push (*it); +Dis[*it] = DIS[TP] +1; - } $Hep.swap (unsed), hep.clear ();//Copy th
adjacency MatrixAn adjacency matrix is a representation of a graph Common storage representations. It uses two arrays to store information about the data element (vertex) and the relationship between the data elements (edges or arcs), respectively. The adjacency matrix A of graph G with order n is n*n. Label the vertex of G as v_1,v_2,..., v_n. if (V_i,v_j) \in E (G), a_{ij}=1, otherwise a_{ij}=0.Depth-firs
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.