Search code examples
algorithmtime-complexitycomplexity-theorytheory

Time complexity of Hill Climbing algorithm for finding local min/max in a graph


What is the time complexity (order of algorithm) of an algorithm that finds the local minimum in a graph with n nodes (having each node a maximum of d neighbors)?

Detail: We have a graph with n nodes. Each node in the graph has an integer value. Each node has maximum of d neighbors. We are looking for a node that has the lowest value among its neighbors. The graph is represented by an adjacency list. The algorithm starts by selecting random nodes and, within these nodes, it selects the node with minimum value (let's say node u). Starting from node u, the algorithm finds a neighbor v, where value(v) < value(u). Then, it continues with v and repeats the above step. The algorithm terminates when the node does not have any neighbor with a lower value. What is the time complexity of this algorithm and why?


Solution

  • Time complexity is O(n + d), because you can have n nodes, which are connected as this, the number shows the value of node :

    16-15-14-13-12-11-10-9-8-7-6-5-4-3-2-1
    

    And you can randomly select these, marked by "!"

    !-!-!-13-12-11-10-9-8-7-6-5-4-3-2-1
    

    So you select the node with value 14 and by described alghoritm, you will check all the nodes and all the edges until you reach the node with value 1.

    The worst complexity for task : "find one element" is O(N), where "N" is the length of your input and length of your input is actually N=G(n,d)=n+d.