Latest update Android YouTube

Discrete Mathematics For AIML - Chapter 10 Practical Discrete Math in Python

Chapter 10 — Practical Discrete Math in Python

This chapter demonstrates how to implement discrete mathematics concepts using Python. From combinatorics and graph algorithms to Boolean logic and probability simulations, practical implementations are key for AI & ML.

10.1 Combinatorics with Python

Python's itertools module makes combinatorial operations simple: permutations, combinations, and Cartesian products. AI/ML Context: Useful for feature selection, generating candidate solutions, and combinatorial optimization.

import itertools

# Permutations of [1,2,3]
perms = list(itertools.permutations([1,2,3]))
print("Permutations:", perms)

# Combinations of 2 elements
combs = list(itertools.combinations([1,2,3], 2))
print("Combinations:", combs)

# Cartesian product
prod = list(itertools.product([0,1], repeat=3))
print("Cartesian Product:", prod)

10.2 Graph Algorithms in Python

Libraries like networkx and igraph allow creating, manipulating, and analyzing graphs. AI/ML Context: Graph-based ML models, social networks, knowledge graphs, and clustering.

import networkx as nx

G = nx.Graph()
G.add_edges_from([(0,1), (1,2), (2,3), (3,0)])
print("Nodes:", G.nodes())
print("Edges:", G.edges())

# BFS traversal
bfs_nodes = list(nx.bfs_tree(G, source=0))
print("BFS Order:", bfs_nodes)

10.3 Boolean Logic and Truth Tables

Boolean logic can be implemented to simulate conditions, rule-based systems, or feature engineering. AI/ML Context: Constructing logical features, decision rules, and binary classifiers.

import itertools

def truth_table(expr_vars):
    n = len(expr_vars)
    table = list(itertools.product([0,1], repeat=n))
    for row in table:
        vals = dict(zip(expr_vars, row))
        result = vals['A'] and not vals['B']  # Example expression: A AND NOT B
        print(row, "->", result)

truth_table(['A','B'])

10.4 Simulating Discrete Probability Distributions

Discrete probability distributions can be simulated using numpy and random. AI/ML Context: Generating datasets, testing algorithms, and modeling uncertainty in discrete events.

import numpy as np

# Simulate a fair die roll
outcomes = [1,2,3,4,5,6]
probabilities = [1/6]*6
samples = np.random.choice(outcomes, size=10, p=probabilities)
print("Die roll samples:", samples)

# Simulate Bernoulli trials
bernoulli = np.random.binomial(n=1, p=0.5, size=10)
print("Bernoulli samples:", bernoulli)

10.5 ML Use Cases

  • Feature selection using combinatorial methods.
  • Graph neural networks and network-based ML tasks.
  • Simulating discrete random variables for testing models.
  • Boolean logic for feature engineering and rule-based AI.

10.6 Exercises

  1. Generate all permutations of [1,2,3,4] and filter those starting with 1.
  2. Create a small graph of 5 nodes and compute BFS and DFS orders.
  3. Implement a truth table for expression (A OR B) AND NOT C.
  4. Simulate 20 rolls of a biased die with probabilities [0.1,0.1,0.2,0.2,0.2,0.2].
Hints / Solutions
  1. Use itertools.permutations and filter by index 0.
  2. Use networkx.Graph() and nx.bfs_tree, nx.dfs_tree.
  3. Iterate over itertools.product([0,1], repeat=3) and evaluate expression.
  4. Use numpy.random.choice() with custom probability distribution.

10.7 Further Reading & Resources

  • Python itertools documentation — combinatorial operations
  • NetworkX documentation — graph analysis and algorithms
  • NumPy random — simulation of discrete distributions
  • Discrete Mathematics and Its Applications, Kenneth Rosen

Post a Comment

Feel free to ask your query...
Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.