The real problem of Alibaba IOS algorithm can’t hang up this time

Time:2022-1-2
The real problem of Alibaba IOS algorithm can't hang up this time

More and more IOS developers continue to enter the peak of job hopping in 2020 Three main trends of interview in 2020: IOS bottom layer, algorithm, data structure and audio and video development Occupied the main battlefield.

Data structure and algorithm interview, especially figure, has become the main reason for the failure of most first-line manufacturers of IOS development Today, I’d like to share a question about Ali’s algorithm interview

The article did not have a complete collection of materials on Ali test questions + bat algorithm. Thank you for your support

Title (real question of Ali interview algorithm):
Suppose there are n vertices at present, and the paths connected by each vertex are different, please design an algorithm to quickly find the path that can cover all vertices; [can be implemented in any programming language]
The real problem of Alibaba IOS algorithm can't hang up this time

Tip: this problem is not to solve the shortest path between 2 points in the graph, but to design a route that can cover all vertices;

I Topic analysis

Most developers get this problem; It is easy to think of solving the shortest path between two points in the graph, but it is not so After careful reading, you will find that the requirement of the topic is to design a route that can cover all vertices; This is to solve the minimum spanning tree in the graph;

The term minimum spanning tree comes from a data [email protected] If you are not familiar with this, you can read data structure (C language version) written by Yan Weimin

II solution
① Idea:
  • Define 2 arrays; Adjvex is used to save related vertex subscripts; Lowcast saves the initialization of weights between vertices
  • 2 arrays, starting from v0 to find the minimum spanning tree. By default, V0 is the first vertex on the minimum spanning tree
  • Loop the lowcast array and find the vertex K according to the weight
  • Update lowcast array
  • Loop through all vertices to find the vertex related to vertex K And update the dynamic diagram of lowcast array and adjvex array:
The real problem of Alibaba IOS algorithm can't hang up this time

Code implementation:

/*Prim algorithm generates minimum spanning tree*/
void MiniSpanTree_Prim(MGraph G)
{
    int min, i, j, k;
    int sum = 0;
    /*Save related vertex Subscripts*/
    int adjvex[MAXVEX];
    /*Save the weights of edges between related vertices*/
    int lowcost[MAXVEX];

    /*Initialize the first weight to 0, that is, V0 is added to the spanning tree*/
    /*The value of lowcast is 0. Here, the vertex of this subscript has been added to the spanning tree*/
    lowcost[0] = 0;

    /*Initialize the first vertex with subscript 0*/
    adjvex[0] = 0;

    //1.  initialization
    For (I = 1; I < g.numvertices; I + +) / * loop all vertices except subscript 0*/
    {
        lowcost[i] = G.arc[0][i];    /*  Store the weights of V0 vertices and edges in the array*/
        adjvex[i] = 0;                    /*  Subscripts initialized to v0*/
    }

    //2.  Loop all vertices except subscript 0 to find the smallest vertex K in the lowcast array
    for(i = 1; i < G.numVertexes; i++)
    {
        /*The minimum initialization weight is ∞*/
        /*It is usually set to an impossible large number, such as 32767, 65535, etc*/
        min = INFINITYC;

        j = 1;k = 0;
        While (J < g.numvertices) / * loop all vertices*/
        {
            /*If the weight is not 0 and the weight is less than min*/
            if(lowcost[j]!=0 && lowcost[j] < min)
            {
                /*Then make the current weight become the minimum value and update min*/
                min = lowcost[j];
                /*Save the subscript of the current minimum value into K*/
                k = j;
            }
            j++;
        }

        /*Prints the edge with the lowest weight among the current vertex edges*/
        printf("(V%d, V%d)=%d\n", adjvex[k], k ,G.arc[adjvex[k]][k]);
        sum+=G.arc[adjvex[k]][k];

        /* 3. Set the weight of the current vertex to 0, indicating that the vertex has completed the task*/
        lowcost[k] = 0;

        /*Loop through all vertices to find the vertex connected to vertex K
         1. Connection with vertex K;
         2. The node is not added to the spanning tree;
         3. If the weight between vertex K and vertex J is less than the weight between vertex J and other vertices, update the lowcast array;

         */
        for(j = 1; j < G.numVertexes; j++)
        {
            /*If the subscript is k, the weight of each edge of the vertex is less than the weight of the previous vertices that have not been added to the spanning tree*/
            if(lowcost[j]!=0 && G.arc[k][j] < lowcost[j])
            {
                /*Store the smaller weight in the corresponding position of lowcast*/
                lowcost[j] = G.arc[k][j];
                /*Save vertices with subscript k into adjvex*/
                adjvex[j] = k;
            }
        }
    }
    printf("sum = %d\n",sum);
}
III solution
② Idea:
  • The adjacency matrix is transformed into an edge table array;
  • The edge table array is sorted from small to large according to the weight;
  • Traverse all the edges and find the connection information of the edges through the parent array; Avoid closed-loop problems;
  • If there is no closed loop problem, it is added to the minimum spanning tree And modify the parent array

Dynamic effect diagram:

The real problem of Alibaba IOS algorithm can't hang up this time

Code implementation:

/*Generate minimum spanning tree*/
void MiniSpanTree_Kruskal(MGraph G)
{
    int i, j, n, m;
    int sum = 0;
    int k = 0;
    /*Defines an array used to determine whether edges and edges form a loop
     Used to record the connection between vertices It can prevent the minimum spanning tree from generating closed loop*/

    int parent[MAXVEX];
    /*Define an edge set array. The structure of edge is begin, end and weight, all of which are integers*/
    Edge edges[MAXEDGE];

    /*1.  Used to build an array of edge sets*/
    for ( i = 0; i < G.numVertexes-1; i++)
    {
        for (j = i + 1; j < G.numVertexes; j++)
        {
            //If the current path weight= ∞
            if (G.arc[i][j]<INFINITYC)
            {
                //Store the start, end and weight corresponding to the path into the edges edge set array
                edges[k].begin = i;
                edges[k].end = j;
                edges[k].weight = G.arc[i][j];

                //Edge set array calculator K + +;
                k++;
            }
        }
    }

    //2.  Sort edge set arrays
    sort(edges, &G);


    //3. Initialize the parent array to 0 9 vertices;
   // for (i = 0; i < G.numVertexes; i++)
    for (i = 0; i < MAXVEX; i++)
        parent[i] = 0;

    //4.  Calculate minimum spanning tree
    Printf ("print minimum spanning tree: \ n");
    /*Each edge of the loop g.numedges has 15 edges*/
    for (i = 0; i < G.numEdges; i++)
    {
        //Get the information of begin and end in the parent array;
        //If n = m, connecting begin and end will produce a closed ring
        n = Find(parent,edges[i].begin);
        m = Find(parent,edges[i].end);
        //printf("n = %d,m = %d\n",n,m);

        /*If n and m are not equal, this side does not form a loop with the existing spanning tree*/
        if (n != m)
        {
            /*Place the end vertex of this edge in the parent whose subscript is the starting point*/
            /*Indicates that this vertex is already in the spanning tree collection*/
            parent[n] = m;

            /*Print minimum spanning tree path*/
            printf("(%d, %d) %d\n", edges[i].begin, edges[i].end, edges[i].weight);
            sum += edges[i].weight;
        }
    }

    printf("sum = %d\n",sum);
}

III summary

The starting points of the two algorithms are completely different; The first strategy is to find and mark at the same time; The second idea is to sort all the target points first Search mark in;

Review materials

Data collection method: add IOS technology sharing group: 1001906160, get it directly!

It’s ready here,Ali interview questions + special topic of bat Algorithm PDF + latest frequently asked IOS interview questions + answers of big factories in 2020

It’s not easy to organize. If you think it’s good, please “like” or comment “mark” at the end of the article. Thank you for your support

Recommended Today

Vue2 technology finishing 3 – Advanced chapter – update completed

3. Advanced chapter preface Links to basic chapters:https://www.cnblogs.com/xiegongzi/p/15782921.html Link to component development:https://www.cnblogs.com/xiegongzi/p/15823605.html 3.1. Custom events of components 3.1.1. Binding custom events There are two implementation methods here: one is to use v-on with vuecomponent$ Emit implementation [PS: this method is a little similar to passing from child to parent]; The other is to use ref […]