# Google Search Algorithm with Python

In this article, I will walk you through how to implement the Google search algorithm with Python. PageRank (PR) is an algorithm used by Google Search to rank websites in their search engine results based on their importance.

## How Google Search Algorithm Works?

The Google Search Algorithm works by counting the number and quality of links to a page to determine a rough estimate of the website’s importance. The underlying assumption is that the most important websites are likely to receive more links from other websites.

Also, Read – Machine Learning Full Course for free.

With the amount of information available on the web, finding what you need would be next to impossible without some help sorting it out. The Google search algorithm is designed for sorting hundreds of billions of web pages in our search index to find the most relevant result in a matter of seconds and showcases them to you in a way that helps you find what you are looking for.

These ranking systems are made up not of one, but a whole series of algorithms. To give you the most useful information, search algorithms look at many factors, including your query words, page relevance and usability, source expertise, your location, and your settings.

The weight applied to each rankings factor varies depending on the nature of your keyword. For example, content freshness plays a bigger role in responding to queries on current hot topics than dictionary definitions.

## Implementing Google Search Algorithm with Python

For the implementation of the Google search algorithm with Python, we must first introduce how to visualize the structure of the World Wide Web. The idea is that WWW can be represented as a huge network, where websites are nodes and their links between them are edges. So let’s set up our WWW in a simplified way, imagining that there are only 4 websites, A, B, C and D.

For this task we need to install a package called networkx, which can be easily installed using the pip command; pip install networkx:

This produces a probability distribution used to represent the probability that a person who clicks randomly on links will arrive at a particular page. Therefore, the output will be a vector with the sum of the components equal to 1 and with the number of components equal to the number of nodes.

The higher the value of the component associated with a node, the more important this node. However, since there are no links between our nodes A, B, C, D, the idea is that, for the moment, all are equally important:

```.wp-block-code {
border: 0;
padding: 0;
}

.wp-block-code > div {
overflow: auto;
}

.shcb-language {
border: 0;
clip: rect(1px, 1px, 1px, 1px);
-webkit-clip-path: inset(50%);
clip-path: inset(50%);
height: 1px;
margin: -1px;
overflow: hidden;
padding: 0;
position: absolute;
width: 1px;
word-wrap: normal;
word-break: normal;
}

.hljs {
box-sizing: border-box;
}

.hljs.shcb-code-table {
display: table;
width: 100%;
}

.hljs.shcb-code-table > .shcb-loc {
color: inherit;
display: table-row;
width: 100%;
}

.hljs.shcb-code-table .shcb-loc > span {
display: table-cell;
}

.wp-block-code code.hljs:not(.shcb-wrap-lines) {
white-space: pre;
}

.wp-block-code code.hljs.shcb-wrap-lines {
white-space: pre-wrap;
}

.hljs.shcb-line-numbers {
border-spacing: 0;
counter-reset: line;
}

.hljs.shcb-line-numbers > .shcb-loc {
counter-increment: line;
}

.hljs.shcb-line-numbers .shcb-loc > span {
padding-left: 0.75em;
}

.hljs.shcb-line-numbers .shcb-loc::before {
border-right: 1px solid #ddd;
content: counter(line);
display: table-cell;
padding: 0 0.75em;
text-align: right;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
white-space: nowrap;
width: 1%;
}
```pr=nx.pagerank(DG, alpha=0.85)
pr```Code language: Python (python)```
`{'A': 0.25, 'B': 0.25, 'C': 0.25, 'D': 0.25}`

As you can see in the output, each node has the importance of 1/4. Now let’s add links between our nodes. Let’s say I want A at point B, B at point C, C at point D, and D at point A. This simply means that each node will transfer the importance to its successor while receiving the importance from its predecessor.

``````DG.add_weighted_edges_from([("A", "B", 1), ("B", "C", 1),("C","D",1),("D","A",1)])
nx.draw(DG,with_labels=True)
plt.show()```Code language: Python (python)```

Now let’s see how our algorithm works on a more complex network structure, let’s say with 10 nodes:

``````G=nx.fast_gnp_random_graph(10,0.5,directed=True)
nx.draw(G,with_labels=True)
plt.show()```Code language: PHP (php)```
``````pr=nx.pagerank(G,alpha=0.85)
rank_vector=np.array([[*pr.values()]])
best_node=np.argmax(rank_vector)
print("The most popular website is {}".format(best_node))```Code language: PHP (php)```
`The most popular website is 4`

Networks are one of the examples of graph algorithms in machine learning. So this is how the Google search algorithm works. Hope you liked this article on how to implement Google search algorithm with Python. Please feel free to ask your valuable questions in the comments section below.

Also, Read – Fake Currency Detection with Machine Learning.