vector<vector<string>> findLadders(string beginWord, string endWord, unordered_set<string> &wordList) {
     queue<string> Q;
     unordered_map<string, bool> visited;
     unordered_map<string, int> level;
     unordered_map<string, unordered_set<string>> parents;
     Q.push(beginWord);
     level[beginWord] = 1;
     parents[beginWord].insert(beginWord);
     int depth = INT_MAX;
     while(!Q.empty()) {
         string node = Q.front();
         visited[node] = true;
         Q.pop();
         if(level[node] > depth) {
             break;
         }
         if(level[node] == depth and node != endWord) {
             continue;    
         }
         if(isNextState(node, endWord)) {
             level[endWord] = level[node] + 1;
             parents[endWord].insert(node);
             Q.push(endWord);
             depth = level[endWord];
             continue;
         }
         vector<string> neighs;
         findAdjList(node, wordList, neighs);
         for(int i = 0; i < (int)neighs.size(); ++i) {
             string neigh = neighs[i];
             if(level[neigh] > 0) {
                 if(level[node] >= level[neigh]) {
                     continue;
                 }
             }
             if(!visited[neigh]) {
                 Q.push(neigh);
                 parents[neigh].insert(node);
                 level[neigh] = level[node] + 1;
                 if(neigh == endWord) {
                     depth = level[neigh];
                 }
             }
         }
     }
     vector<vector<string>> result;
     if(depth == INT_MAX) return result;
     vector<string> solution(depth, "");
     findShortestPaths(endWord, depth - 1, parents, solution, result);
     return result;
 }
Exemplo n.º 2
0
// The main function that constructs Minimum Spanning Tree (MST)
// using Prim's algorithm
void PrimMST(Graph* graph)
{
    int MSTWeight = 0;
    int V = graph->V;// Get the number of vertices in graph
    int E = graph->E;// Get the number of edges in graph
    int parent[V];   // Array to store constructed MST
    int key[V];      // Key values used to pick minimum weight edge in cut
 
    // minHeap represents set E
    MinHeap* minHeap = createMinHeap(V);
 
    // Initialize min heap with all vertices. Key value of
    // all vertices (except 0th vertex) is initially infinite

    // print edges of MST
    printf("===============================================\n");
    printf("Following are the edges in the constructed MST\n");

    for (int v = 1; v < V; ++v)
    {
        parent[v] = -1;
        key[v] = INT_MAX;
        minHeap->array[v] = newMinHeapNode(v, key[v]);
        minHeap->pos[v] = v;
    }
 
    // Make key value of 0th vertex as 0 so that it
    // is extracted first
    key[0] = 0;
    minHeap->array[0] = newMinHeapNode(0, key[0]);
    minHeap->pos[0]   = 0;
 
    // Initially size of min heap is equal to V
    minHeap->size = V;
 
    // In the followin loop, min heap contains all nodes
    // not yet added to MST.
    while (!isEmpty(minHeap))
    {
        // Extract the vertex with minimum key value
        MinHeapNode* minHeapNode = extractMin(minHeap);
        int u = minHeapNode->v; // Store the extracted vertex number
        // Traverse through all adjacent vertices of u (the extracted
        // vertex) and update their key values
	int i;
	int* adjList = findAdjList(graph,u);
        int size = findAdjListCount(graph, u, NULL, 0);
	//printf("%d->%d\n",adjList[0],adjList[1]);
        //printf("%d\n",size);
	for (i=1;i<size;i++) 
        {
	    int v = adjList[i];
	    // printf("%d\n",v);   
            // If v is not yet included in MST and weight of u-v is
            // less than key value of v, then update key value and
            // parent of v
            if (isInMinHeap(minHeap, v) && findWeight(graph,u,v) < key[v])
            {
                key[v] = findWeight(graph,u,v);
                parent[v] = u;
                decreaseKey(minHeap, v, key[v]);
	    }
	 }
	 MSTWeight += minHeapNode->key;
    }
    int n;
    for (n=1;n<V;n++) {
       printf("Edge: %d -- %d (Weight: %d)\n",parent[n],n,key[n]);
    }
    printf("Total Weight: %d\n", MSTWeight);
    printf("===============================================\n");
}