#12 Page Rank & Collatz Conjuncture

NPTEL Study Hub | PageRank & Collatz Conjecture
✦ Week 12 · Module 12 · Exam Notes

Think clearly.
Code confidently.

A focused, student-friendly guide to the Collatz Conjecture, PageRank, graph terminology, and NetworkX implementations—with code snippets, quick revision tables, and a self-test.

Start learning ↓
01 · CollatzRules, sequence tracing, iteration logic
02 · PageRankDirected graphs, random walk, points
03 · Python LabNetworkX syntax and exam-ready code
01

Collatz Conjecture (3n + 1 Problem)

A simple rule-based sequence with an unsolved mathematical question.

Start with any positive integer n. Repeatedly apply the even or odd rule. The conjecture states that every positive integer eventually reaches 1, although a general proof is still unknown.

If n is even
nnext = n / 2
Divide the number by two.
If n is odd
nnext = 3n + 1
Multiply by three and add one.
Exam point: Use a stopping condition such as while n != 1. Otherwise, continuing the rules from 1 creates the cycle 1 → 4 → 2 → 1.

Algorithm in plain English

  1. Read a positive integer n.
  2. While n is not 1, test whether it is even.
  3. If even, replace it with n // 2; otherwise replace it with 3*n + 1.
  4. Count each transformation and stop when n = 1.
02

Sequence tracing & step counts

The starting number's size does not directly determine the number of steps.

Example A — starting number n = 7

7 → 22 → 11 → 34 → 17 → 52 → 26 → 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1

Example B — starting number n = 256

256 → 128 → 64 → 32 → 16 → 8 → 4 → 2 → 1
Starting nObservationTakeaway
7Sequence rises and falls before reaching 1.A small input can take many transformations.
256 = 28Repeated halving reaches 1 quickly.A larger input can take fewer transformations.
Count carefully in exams: The displayed sequence for 7 contains 16 transitions (17 listed values), and the sequence for 256 contains 8 transitions (9 listed values). Some notes may count the starting/ending values differently, so state clearly whether you count transformations or sequence terms.

Computer simulations can test many starting values, but testing alone does not prove that every positive integer eventually reaches 1.

03

PageRank: the central idea

Ranking web pages by considering both links and the importance of the linking pages.

PageRank is a link-analysis algorithm associated with web search. It does not simply count incoming links; it also considers the importance of the pages providing those links.

Remember this analogy: A recommendation from a highly respected expert may carry more weight than many recommendations from unknown sources.
A
→
B
→
C
A directed link transfers attention or rank from a source page to its target.
Incoming links matterLink source mattersIterative computation

Applications: web-page ranking, citation networks, social networks, and other systems represented as linked nodes.

04

Graph terminology & NetworkX graph types

Represent real-world relationships as nodes and edges.

TermMeaningExample
Node / vertexAn entity in the network.Web page, person, research paper
EdgeA connection between entities.Hyperlink, citation, transaction
Undirected graphEdges have no direction; connection is mutual.Railway links, friendship network (simplified)
Directed graph (DiGraph)Edges run from source to target.A web page links to another page
In-edgeAn edge entering a node.Incoming links to page B
Out-edgeAn edge leaving a node.Links from page B to other pages
Sink nodeA node with no outgoing edges.A page with no outgoing links

Common built-in graph generators

FunctionGraph shape / purpose
nx.barbell_graph(m1, m2)Two complete groups joined by a path.
nx.complete_graph(n)Every node connects to every other node.
nx.cycle_graph(n)Nodes form a closed cycle.
nx.ladder_graph(n)Two parallel paths connected like ladder rungs.
nx.path_graph(n)A simple linear path.
nx.star_graph(n)A central hub connects to n outer nodes; total nodes = n + 1.
nx.wheel_graph(n)A hub connected to nodes in a cycle.
nx.gnp_random_graph(n, p)Random graph where edges are added with probability p.
05

PageRank calculation methods

Understand the intuition, the steps, and how sinks are handled.

A. Random Walk method

  1. Initialize each node's visit counter to zero.
  2. Choose an initial node at random and count the visit.
  3. Find the current node's outgoing neighbours.
  4. If it has neighbours, choose one at random and move there.
  5. If it is a sink, choose a node randomly from the whole graph.
  6. Repeat many times; normalized visit frequencies estimate node importance.
Sink handling: In the supplied random-walk approach, a sink triggers a random jump to any node so the walk can continue.

B. Points Distribution method

  1. Give each node an initial score (for example, 100 points).
  2. Each node with outgoing edges divides its points equally among its outgoing neighbours.
  3. Each target sums the shares it receives.
  4. Repeat the redistribution for many rounds and inspect the scores.
Share per target = Points(node) / number of outgoing edges
Important implementation detail: The supplied notes say sink nodes retain their points. This rule can affect total-score distribution when other nodes also send points to them; do not assume point conservation for every sink-handling scheme without checking the exact update rule.

Convergence

Repeated iterations may settle into stable scores. Exact behavior depends on graph structure and the update rules. NetworkX's built-in nx.pagerank(G) uses a standard PageRank formulation with damping and dangling-node handling, so it may not match a simplified custom algorithm exactly.

06

Python & NetworkX lab

Core syntax to recognize and reproduce in practical or theory exams.

1. Collatz function

Python · Collatz sequence
def check_numb(numb):
    iterations = 0

    while numb != 1:
        if numb % 2 == 0:
            numb = numb // 2
        else:
            numb = 3 * numb + 1
        iterations += 1

    print("Final number:", numb)
    print("Iterations taken:", iterations)

check_numb(12)
check_numb(20)

This version counts transformations (loop iterations). For positive integer input, integer division // keeps the result as an integer.

2. Create a directed graph and inspect edges

Python · Directed graph basics
import networkx as nx

G = nx.DiGraph()
G.add_nodes_from(range(5))  # Nodes 0, 1, 2, 3, 4
G.add_edge(1, 2)            # Directed edge 1 -> 2
G.add_edge(3, 4)            # Directed edge 3 -> 4

out_edges = list(G.out_edges(1))
in_edges = list(G.in_edges(2))

print("Outgoing:", out_edges)
print("Incoming:", in_edges)

3. Random walk PageRank approximation

Python · Random walk
import networkx as nx
import random

G = nx.gnp_random_graph(10, 0.5, directed=True)
nodes = list(G.nodes())
x = random.choice(nodes)
visits = {node: 0 for node in nodes}
visits[x] += 1

iterations = 100_000
for _ in range(iterations):
    neighbours = list(G.neighbors(x))
    if not neighbours:             # Sink node
        x = random.choice(nodes)    # Random jump
    else:
        x = random.choice(neighbours)
    visits[x] += 1

total = sum(visits.values())
approx = {node: count / total for node, count in visits.items()}
print("Random-walk visit frequencies:", approx)
print("NetworkX PageRank:", nx.pagerank(G))

4. Built-in PageRank and edge-list file

Python · Read data and rank
import networkx as nx
import matplotlib.pyplot as plt

G = nx.read_edgelist(
    "page_rank.txt",
    create_using=nx.DiGraph,
    nodetype=int
)

nx.draw(G, with_labels=True)
plt.show()

pr_dict = nx.pagerank(G)
print(pr_dict)
Edge-list file format: Put one directed edge per line, with source and target separated by whitespace, for example 1 2 means an edge from node 1 to node 2.

5. Points distribution example

Python · Simplified points redistribution
def distribute_points(G, points):
    nodes = list(G.nodes())
    new_points = {node: 0 for node in nodes}

    for node in nodes:
        targets = list(G.successors(node))
        if not targets:
            new_points[node] += points[node]  # Sink retains points
        else:
            share = points[node] / len(targets)
            for target in targets:
                new_points[target] += share

    return new_points

points = {node: 100 for node in G.nodes()}
for _ in range(100):
    points = distribute_points(G, points)

print("Points:", points)
print("Built-in PageRank:", nx.pagerank(G))

The code illustrates the sink-retention rule described in the notes. It is a simplified redistribution model, not identical to the standard PageRank algorithm.

07

Quick revision table

Last-minute facts and syntax at a glance.

Concept / functionPurpose / ruleSyntax or formula
Collatz even ruleHalve an even numbern = n // 2
Collatz odd ruleTriple and add onen = 3*n + 1
Collatz stopping conditionStop at 1while n != 1:
PageRank principleImportance depends on incoming links and their source importanceLink analysis
Sink nodeNo outgoing edgeslen(list(G.successors(n))) == 0
Directed graphCreate a directed graphG = nx.DiGraph()
Outgoing edgesGet links leaving a nodeG.out_edges(n)
Incoming edgesGet links entering a nodeG.in_edges(n)
Built-in PageRankCalculate PageRank scoresnx.pagerank(G)
Read edge listLoad graph from a filenx.read_edgelist(...)

Study checklist

08

Self-test before the exam

Try answering without looking at the notes, then reveal the answer.

1. What is the next number after 7 in the Collatz sequence?

2. Which NetworkX class creates a directed graph?

3. What is a sink node?

4. Which function computes PageRank in NetworkX?

NPTEL Study Hub
Learn one concept at a time. Practise the code. Revise with confidence.
Study notes based on the supplied / Module 12 material.

Comments

Popular posts from this blog

#11 Calendar Arithmetic & Browser Automation with Selenium

#10 NumPy, Flames, Digital images & compression