SlideShare a Scribd company logo
I want to become a
programming God
How to implement any data structures fast in Python?
Java
C++
Python
Matlab
Programming Languages… Convex Optimization
Competitive Programming
Data Structures
1
2 3
4 5
𝑀 =
0 0 0 0 0
1 0 0 0 0
1
0
0
0
0
0
0 0 0
1 0 0
1 0 0
M(final)=𝛿>0( 𝑖=1
∞
𝑀
𝑖
)
Transitive Closure
Algorithm
Sparse Matrix
• Any data structures can be represented in dense or sparse matrix
• Data structures are
• List
• Queue
• Tree
• Graph
• Hash Table
• …
• Sparse Matrix can be easily created in Python
Sparse Matrix in Python
# Python program to multpliply two
# csc matrices using multiply()
# Import required libraries
import numpy as np
from scipy.sparse import csc_matrix
# Create first csc matrix A
row_A = np.array([0, 0, 1, 2 ])
col_A = np.array([0, 1, 0, 1])
data_A = np.array([4, 3, 8, 9])
cscMatrix_A = csc_matrix((data_A,
(row_A, col_A)),
shape = (3, 3))
# print first csc matrix
print("first csc matrix: n",
cscMatrix_A.toarray())
# Create second csc matrix B
row_B = np.array([0, 1, 1, 2 ])
col_B = np.array([0, 0, 1, 0])
data_B = np.array([7, 2, 5, 1])
cscMatrix_B = csc_matrix((data_B, (row_B,
col_B)),
shape = (3, 3))
# print second csc matrix
print("second csc matrix:n",
cscMatrix_B.toarray())
# Multiply these matrices
sparseMatrix_AB =
cscMatrix_A.multiply(cscMatrix_B)
# print resultant matrix
print("Product Sparse Matrix:n",
sparseMatrix_AB.toarray())
Matlab or Python?
• When I started writing codes in Matlab, I felt in love with it
• It can do any array operations using indexing syntax, matrix and vector
• It eliminates the for loop in array operations
• Later I found out that Numpy library in Python can do the same
• And even more with more functions in Numpy
• Forget about old school Java programming language where data
structures are implemented in Objects with methods to operate on
the data structures
List and Queue
• List can be represented in a vectors
• Or it can be represented in the sparse matrix M
• mij is an element of matrix M
• mij is 1 if item j of list is connected to item i
• Item i is the next item of item j
• Queue is represented in the same approach
Tree and Graph
• Tree and Graph can be represented by a set of directional edges that
connects the items (or vertices) together
• The objects of the items are stored in a vector
• Example:
1
2 3
4 5
𝑀 =
0 0 0 0 0
1 0 0 0 0
1
0
0
0
0
0
0 0 0
1 0 0
1 0 0
M is a sparse Matrix
Item 1: John
Item 2: Peter
Item 3: Eric
Item 4: Ken
Item 5: Bob
𝑣 =
′𝐽𝑜ℎ𝑛′
′𝑃𝑒𝑡𝑒𝑟′
𝐸𝑟𝑖𝑐′
′𝐾𝑒𝑛′
′𝐵𝑜𝑏′
Vector containing object
• Navigating through a tree can be represented by matrix multiplication
• 𝑀𝑎 =
0 0 0 0 0
1 0 0 0 0
1
0
0
0
0
0
0 0 0
1 0 0
1 0 0
1
0
0
0
0
=
0
1
1
0
0
Immediate children of item 1
Level 1 items of tree
1
2 3
4 5
Level 1
• Navigating through a tree can be represented by matrix multiplication
• 𝑀𝑀𝑎 =
0 0 0 0 0
1 0 0 0 0
1
0
0
0
0
0
0 0 0
1 0 0
1 0 0
0 0 0 0 0
1 0 0 0 0
1
0
0
0
0
0
0 0 0
1 0 0
1 0 0
1
0
0
0
0
=
0
0
0
1
1
Immediate children of item 1
Level 2 items of tree
1
2 3
4 5
Level 2
• Navigating through a tree can be represented by matrix multiplication
• 𝑀𝑀𝑎 =
0 0 0 0 0
1 0 0 0 0
1
0
0
0
0
0
0 0 0
1 0 0
1 0 0
0 0 0 0 0
1 0 0 0 0
1
0
0
0
0
0
0 0 0
1 0 0
1 0 0
0 0 0 0 0
1 0 0 0 0
1
0
0
0
0
0
0 0 0
1 0 0
1 0 0
1
0
0
0
0
=
0
0
0
0
0
Immediate children of item 1
Level 3 items of tree
No items at level 3
1
2 3
4 5
Level 3
• For a graph, the transitive closure is simply the repeated application
of matrix multiplication
• 𝑀 =
0 0 0 0 0
1 0 0 0 0
1
0
0
0
0
0
0 0 0
1 0 0
1 0 0
1
2 3
4 5
• 1 step of transitive closure
• 𝑀𝑀 + 𝑀 =
0 0 0 0 0
1 0 0 0 0
1
0
0
0
0
0
0 0 0
1 0 0
1 0 0
0 0 0 0 0
1 0 0 0 0
1
0
0
0
0
0
0 0 0
1 0 0
1 0 0
+
0 0 0 0 0
1 0 0 0 0
1
0
0
0
0
0
0 0 0
1 0 0
1 0 0
• =
0 0 0 0 0
1 0 0 0 0
1
1
1
0
0
0
0 0 0
1 0 0
1 0 0
1
2 3
4 5
• 2 step of transitive closure
• 𝑀𝑀𝑀 + 𝑀𝑀 + 𝑀
• =
0 0 0 0 0
1 0 0 0 0
1
1
1
0
0
0
0 0 0
1 0 0
1 0 0
1
2 3
4 5
• Transitive closure algorithm in matrix representation is simply
• M(final)=𝛿>0( 𝑖=1
∞
𝑀𝑖)
• Can be written as M(final)=𝛿>0( 𝑀 + 𝐼 𝑖 − 𝐼), where I is an identity matrix
• No need to compute until infinity, only need to compute until the matrix converges
• It means matrix M raised to the power of infinity (M multiplied to itself infinite times)
• And the final matrix after multiplication is thresholded (𝛿>0()) element by element,
• Each element is threshold to 1 if it is greater than 0, else it is threshold to 0
• In the example, M(final) =
0 0 0 0 0
1 0 0 0 0
1
1
1
0
0
0
0 0 0
1 0 0
1 0 0
Algebraic Representation of Algorithm
• I believe a super programming
will represent an algorithm in
higher order abstract form
(algebraic form)
• It is much more cleaner and
easier to understand
• The algorithm can be analyzed
symbolically
• The properties of the algorithm
can be derived algebraically
Begin
copy the adjacency matrix into another matrix named
transMat
for any vertex k in the graph, do
for each vertex i in the graph, do
for each vertex j in the graph, do
transMat[i, j] := transMat[i, j] OR (transMat[i, k]) AND
transMat[k, j])
done
done
done
Display the transMat
End
If represented in pseudo codes, it will look very messy.
The algebraic matrix notation of the transitive closure algorithm
looks better
Transitive Closure Algorithm:
Convex Optimization representation of
Algorithms
• I like the convex optimization algorithm, because it is represented so
succinctly in algebraic form. And it is so beautiful
• Minimize f(x)
• Such that g(x)=0
• h(x)>=0
• If all algorithms can be represented in this format, it will be so beautiful
• I did some formulation to represent algorithms in convex optimization format
• https://www.slideshare.net/SingKuangTan/discrete-markov-random-field-relaxation
• https://vixra.org/abs/2112.0151
Competitive Programming
• I hope my sparse matrix approach to implement data structure will
help to speed up programming in competition
• Hope that my convex optimization representation of algorithm will also help
• I have not take part in programming competition before
• Would like to if given a chance
• I have did so much programming before and studied my different algorithms…
Links to my papers
● https://vixra.org/author/sing_kuang_tan
● Link to my NP vs P paper
● And Discrete Markov Random Field relaxation paper
About Me
● My job uses Machine Learning to solve problems
○ Like my posts or slides in LinkedIn, Twitter or Slideshare
○ Follow me on LinkedIn
■ https://www.linkedin.com/in/sing-kuang-tan-b189279/
○ Follow me on Twitter
■ https://twitter.com/Tan_Sing_Kuang
○ Send me comments through these links
● Look at my Slideshare slides
○ https://www.slideshare.net/SingKuangTan
■ NP vs P Proof using Discrete Finite Automata
■ Use Inductive or Deductive Logic to solve NP vs P?
■ Kung Fu Computer Science, Clique Problem: Step by Step
■ Beyond Shannon, Sipser and Razborov; Solve Clique Problem like an Electronic Engineer
■ A weird Soviet method to partially solve the Perebor Problems
■ 8 trends in Hang Seng Index
■ 4 types of Mathematical Proofs
■ How I prove NP vs P
○ Follow me on Slideshare
Share my links
● I am a Small Person with Big Dreams
○ Please help me to repost my links to other platforms so that I can spread my ideas to the rest of the world
● 我人小,但因梦想而伟大。
○ 请帮我的文件链接传发到其他平台,让我的思想能传遍天下。
● Comments? Send to singkuangtan@gmail.com
● Link to my paper NP vs P paper
○ https://www.slideshare.net/SingKuangTan/brief-np-vspexplain-249524831
○ Prove Np not equal P using Markov Random Field and Boolean Algebra Simplification
○ https://vixra.org/abs/2105.0181
○ Other link
■ https://www.slideshare.net/SingKuangTan

More Related Content

What's hot

TYPES OF sYMMETRY
TYPES OF sYMMETRYTYPES OF sYMMETRY
TYPES OF sYMMETRY
agabo75
 
Lines of Symmetry Introduction
Lines of Symmetry IntroductionLines of Symmetry Introduction
Lines of Symmetry Introduction
Jeff Whipple
 
Great Polygon Powerpoint
Great Polygon PowerpointGreat Polygon Powerpoint
Great Polygon Powerpoint
JC Sawyer Dolphins
 
CLASS VI MATHS SYMMETRY
CLASS VI MATHS SYMMETRYCLASS VI MATHS SYMMETRY
CLASS VI MATHS SYMMETRY
Rc Os
 
Basics of Graphs Theory
Basics of Graphs TheoryBasics of Graphs Theory
Basics of Graphs Theory
Mariya Zaki
 
Symmetry PowerPoint
Symmetry PowerPointSymmetry PowerPoint
Symmetry PowerPointbwrigh2
 
Patterns Symmetry in Math_ SUNY Cortland_072010
Patterns  Symmetry in Math_ SUNY Cortland_072010Patterns  Symmetry in Math_ SUNY Cortland_072010
Patterns Symmetry in Math_ SUNY Cortland_072010
Lee M. Kaltman
 
symmetry for class 7
symmetry for class 7symmetry for class 7
symmetry for class 7
ABHINANDAN Lalit
 
Lines of Symmetry Lesson
Lines of Symmetry LessonLines of Symmetry Lesson
Lines of Symmetry Lesson
Courtney Alexander
 
Kungfu math p4 slide23 (symmetry)pdf
Kungfu math p4 slide23 (symmetry)pdfKungfu math p4 slide23 (symmetry)pdf
Kungfu math p4 slide23 (symmetry)pdfkungfumath
 
Line symmetry
Line symmetryLine symmetry
Line symmetry
vandys
 

What's hot (12)

Line symmetry
Line symmetryLine symmetry
Line symmetry
 
TYPES OF sYMMETRY
TYPES OF sYMMETRYTYPES OF sYMMETRY
TYPES OF sYMMETRY
 
Lines of Symmetry Introduction
Lines of Symmetry IntroductionLines of Symmetry Introduction
Lines of Symmetry Introduction
 
Great Polygon Powerpoint
Great Polygon PowerpointGreat Polygon Powerpoint
Great Polygon Powerpoint
 
CLASS VI MATHS SYMMETRY
CLASS VI MATHS SYMMETRYCLASS VI MATHS SYMMETRY
CLASS VI MATHS SYMMETRY
 
Basics of Graphs Theory
Basics of Graphs TheoryBasics of Graphs Theory
Basics of Graphs Theory
 
Symmetry PowerPoint
Symmetry PowerPointSymmetry PowerPoint
Symmetry PowerPoint
 
Patterns Symmetry in Math_ SUNY Cortland_072010
Patterns  Symmetry in Math_ SUNY Cortland_072010Patterns  Symmetry in Math_ SUNY Cortland_072010
Patterns Symmetry in Math_ SUNY Cortland_072010
 
symmetry for class 7
symmetry for class 7symmetry for class 7
symmetry for class 7
 
Lines of Symmetry Lesson
Lines of Symmetry LessonLines of Symmetry Lesson
Lines of Symmetry Lesson
 
Kungfu math p4 slide23 (symmetry)pdf
Kungfu math p4 slide23 (symmetry)pdfKungfu math p4 slide23 (symmetry)pdf
Kungfu math p4 slide23 (symmetry)pdf
 
Line symmetry
Line symmetryLine symmetry
Line symmetry
 

Similar to Implement Data Structures with Python Fast using Sparse Matrix

Understanding Basics of Machine Learning
Understanding Basics of Machine LearningUnderstanding Basics of Machine Learning
Understanding Basics of Machine Learning
Pranav Ainavolu
 
AP Advantage: AP Calculus
AP Advantage: AP CalculusAP Advantage: AP Calculus
AP Advantage: AP Calculus
Shashank Patil
 
Linear Algebra and Matlab tutorial
Linear Algebra and Matlab tutorialLinear Algebra and Matlab tutorial
Linear Algebra and Matlab tutorial
Jia-Bin Huang
 
Machine Learning on Azure - AzureConf
Machine Learning on Azure - AzureConfMachine Learning on Azure - AzureConf
Machine Learning on Azure - AzureConf
Seth Juarez
 
ESCM303 Introduction to Python Programming.pptx
ESCM303 Introduction to Python Programming.pptxESCM303 Introduction to Python Programming.pptx
ESCM303 Introduction to Python Programming.pptx
AvijitChaudhuri3
 
Lines and planes in space
Lines and planes in spaceLines and planes in space
Lines and planes in space
Faizan Shabbir
 
Undecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWER
Undecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWERUndecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWER
Undecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWER
muthukrishnavinayaga
 
2. Chap 1.pptx
2. Chap 1.pptx2. Chap 1.pptx
2. Chap 1.pptx
HassanShah396906
 
Number Crunching in Python
Number Crunching in PythonNumber Crunching in Python
Number Crunching in Python
Valerio Maggio
 
Matlab pt1
Matlab pt1Matlab pt1
Matlab pt1
Austin Baird
 
Programming in python
Programming in pythonProgramming in python
Programming in python
Ivan Rojas
 
Computability and Complexity
Computability and ComplexityComputability and Complexity
Computability and Complexity
Edward Blurock
 
Undecidable Problems and Approximation Algorithms
Undecidable Problems and Approximation AlgorithmsUndecidable Problems and Approximation Algorithms
Undecidable Problems and Approximation Algorithms
Muthu Vinayagam
 
Search algorithms master
Search algorithms masterSearch algorithms master
Search algorithms master
Hossam Hassan
 
Matlab Tutorial.ppt
Matlab Tutorial.pptMatlab Tutorial.ppt
Matlab Tutorial.ppt
RaviMuthamala1
 
SVD.ppt
SVD.pptSVD.ppt
SVD.ppt
cmpt cmpt
 
lec08-numpy.pptx
lec08-numpy.pptxlec08-numpy.pptx
lec08-numpy.pptx
lekha572836
 
Introduction to Matlab - Basic Functions
Introduction to Matlab - Basic FunctionsIntroduction to Matlab - Basic Functions
Introduction to Matlab - Basic Functions
joellivz
 
Class13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfClass13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdf
AkashSingh625550
 
python ppt
python pptpython ppt
python ppt
EmmanuelMMathew
 

Similar to Implement Data Structures with Python Fast using Sparse Matrix (20)

Understanding Basics of Machine Learning
Understanding Basics of Machine LearningUnderstanding Basics of Machine Learning
Understanding Basics of Machine Learning
 
AP Advantage: AP Calculus
AP Advantage: AP CalculusAP Advantage: AP Calculus
AP Advantage: AP Calculus
 
Linear Algebra and Matlab tutorial
Linear Algebra and Matlab tutorialLinear Algebra and Matlab tutorial
Linear Algebra and Matlab tutorial
 
Machine Learning on Azure - AzureConf
Machine Learning on Azure - AzureConfMachine Learning on Azure - AzureConf
Machine Learning on Azure - AzureConf
 
ESCM303 Introduction to Python Programming.pptx
ESCM303 Introduction to Python Programming.pptxESCM303 Introduction to Python Programming.pptx
ESCM303 Introduction to Python Programming.pptx
 
Lines and planes in space
Lines and planes in spaceLines and planes in space
Lines and planes in space
 
Undecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWER
Undecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWERUndecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWER
Undecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWER
 
2. Chap 1.pptx
2. Chap 1.pptx2. Chap 1.pptx
2. Chap 1.pptx
 
Number Crunching in Python
Number Crunching in PythonNumber Crunching in Python
Number Crunching in Python
 
Matlab pt1
Matlab pt1Matlab pt1
Matlab pt1
 
Programming in python
Programming in pythonProgramming in python
Programming in python
 
Computability and Complexity
Computability and ComplexityComputability and Complexity
Computability and Complexity
 
Undecidable Problems and Approximation Algorithms
Undecidable Problems and Approximation AlgorithmsUndecidable Problems and Approximation Algorithms
Undecidable Problems and Approximation Algorithms
 
Search algorithms master
Search algorithms masterSearch algorithms master
Search algorithms master
 
Matlab Tutorial.ppt
Matlab Tutorial.pptMatlab Tutorial.ppt
Matlab Tutorial.ppt
 
SVD.ppt
SVD.pptSVD.ppt
SVD.ppt
 
lec08-numpy.pptx
lec08-numpy.pptxlec08-numpy.pptx
lec08-numpy.pptx
 
Introduction to Matlab - Basic Functions
Introduction to Matlab - Basic FunctionsIntroduction to Matlab - Basic Functions
Introduction to Matlab - Basic Functions
 
Class13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfClass13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdf
 
python ppt
python pptpython ppt
python ppt
 

More from Sing Kuang Tan

neuron_never_overfits.pptx
neuron_never_overfits.pptxneuron_never_overfits.pptx
neuron_never_overfits.pptx
Sing Kuang Tan
 
Square Peg Problem Test Function
Square Peg Problem Test FunctionSquare Peg Problem Test Function
Square Peg Problem Test Function
Sing Kuang Tan
 
Visual topology
Visual topologyVisual topology
Visual topology
Sing Kuang Tan
 
Discrete Markov Random Field Relaxation
Discrete Markov Random Field RelaxationDiscrete Markov Random Field Relaxation
Discrete Markov Random Field Relaxation
Sing Kuang Tan
 
NP vs P Proof using Deterministic Finite Automata
NP vs P Proof using Deterministic Finite AutomataNP vs P Proof using Deterministic Finite Automata
NP vs P Proof using Deterministic Finite Automata
Sing Kuang Tan
 
Use Inductive or Deductive Logic to solve NP vs P?
Use Inductive or Deductive Logic to solve NP vs P?Use Inductive or Deductive Logic to solve NP vs P?
Use Inductive or Deductive Logic to solve NP vs P?
Sing Kuang Tan
 
Clique problem step_by_step
Clique problem step_by_stepClique problem step_by_step
Clique problem step_by_step
Sing Kuang Tan
 
Beyond Shannon, Sipser and Razborov; Solve Clique Problem like an Electronic ...
Beyond Shannon, Sipser and Razborov; Solve Clique Problem like an Electronic ...Beyond Shannon, Sipser and Razborov; Solve Clique Problem like an Electronic ...
Beyond Shannon, Sipser and Razborov; Solve Clique Problem like an Electronic ...
Sing Kuang Tan
 
A Weird Soviet Method to Partially Solve the Perebor Problem
A Weird Soviet Method to Partially Solve the Perebor ProblemA Weird Soviet Method to Partially Solve the Perebor Problem
A Weird Soviet Method to Partially Solve the Perebor Problem
Sing Kuang Tan
 
NP vs P 的简要说明。 使用马尔可夫随机场和布尔代数简化证明 Np 不等于 P
NP vs P 的简要说明。 使用马尔可夫随机场和布尔代数简化证明 Np 不等于 PNP vs P 的简要说明。 使用马尔可夫随机场和布尔代数简化证明 Np 不等于 P
NP vs P 的简要说明。 使用马尔可夫随机场和布尔代数简化证明 Np 不等于 P
Sing Kuang Tan
 
Hang Seng Index 8 trends
Hang Seng Index 8 trendsHang Seng Index 8 trends
Hang Seng Index 8 trends
Sing Kuang Tan
 
Mathematical Proof types
Mathematical Proof typesMathematical Proof types
Mathematical Proof types
Sing Kuang Tan
 
Brief explanation of NP vs P. Prove Np not equal P using Markov Random Field ...
Brief explanation of NP vs P. Prove Np not equal P using Markov Random Field ...Brief explanation of NP vs P. Prove Np not equal P using Markov Random Field ...
Brief explanation of NP vs P. Prove Np not equal P using Markov Random Field ...
Sing Kuang Tan
 

More from Sing Kuang Tan (13)

neuron_never_overfits.pptx
neuron_never_overfits.pptxneuron_never_overfits.pptx
neuron_never_overfits.pptx
 
Square Peg Problem Test Function
Square Peg Problem Test FunctionSquare Peg Problem Test Function
Square Peg Problem Test Function
 
Visual topology
Visual topologyVisual topology
Visual topology
 
Discrete Markov Random Field Relaxation
Discrete Markov Random Field RelaxationDiscrete Markov Random Field Relaxation
Discrete Markov Random Field Relaxation
 
NP vs P Proof using Deterministic Finite Automata
NP vs P Proof using Deterministic Finite AutomataNP vs P Proof using Deterministic Finite Automata
NP vs P Proof using Deterministic Finite Automata
 
Use Inductive or Deductive Logic to solve NP vs P?
Use Inductive or Deductive Logic to solve NP vs P?Use Inductive or Deductive Logic to solve NP vs P?
Use Inductive or Deductive Logic to solve NP vs P?
 
Clique problem step_by_step
Clique problem step_by_stepClique problem step_by_step
Clique problem step_by_step
 
Beyond Shannon, Sipser and Razborov; Solve Clique Problem like an Electronic ...
Beyond Shannon, Sipser and Razborov; Solve Clique Problem like an Electronic ...Beyond Shannon, Sipser and Razborov; Solve Clique Problem like an Electronic ...
Beyond Shannon, Sipser and Razborov; Solve Clique Problem like an Electronic ...
 
A Weird Soviet Method to Partially Solve the Perebor Problem
A Weird Soviet Method to Partially Solve the Perebor ProblemA Weird Soviet Method to Partially Solve the Perebor Problem
A Weird Soviet Method to Partially Solve the Perebor Problem
 
NP vs P 的简要说明。 使用马尔可夫随机场和布尔代数简化证明 Np 不等于 P
NP vs P 的简要说明。 使用马尔可夫随机场和布尔代数简化证明 Np 不等于 PNP vs P 的简要说明。 使用马尔可夫随机场和布尔代数简化证明 Np 不等于 P
NP vs P 的简要说明。 使用马尔可夫随机场和布尔代数简化证明 Np 不等于 P
 
Hang Seng Index 8 trends
Hang Seng Index 8 trendsHang Seng Index 8 trends
Hang Seng Index 8 trends
 
Mathematical Proof types
Mathematical Proof typesMathematical Proof types
Mathematical Proof types
 
Brief explanation of NP vs P. Prove Np not equal P using Markov Random Field ...
Brief explanation of NP vs P. Prove Np not equal P using Markov Random Field ...Brief explanation of NP vs P. Prove Np not equal P using Markov Random Field ...
Brief explanation of NP vs P. Prove Np not equal P using Markov Random Field ...
 

Recently uploaded

(May 29th, 2024) Advancements in Intravital Microscopy- Insights for Preclini...
(May 29th, 2024) Advancements in Intravital Microscopy- Insights for Preclini...(May 29th, 2024) Advancements in Intravital Microscopy- Insights for Preclini...
(May 29th, 2024) Advancements in Intravital Microscopy- Insights for Preclini...
Scintica Instrumentation
 
Comparative structure of adrenal gland in vertebrates
Comparative structure of adrenal gland in vertebratesComparative structure of adrenal gland in vertebrates
Comparative structure of adrenal gland in vertebrates
sachin783648
 
Citrus Greening Disease and its Management
Citrus Greening Disease and its ManagementCitrus Greening Disease and its Management
Citrus Greening Disease and its Management
subedisuryaofficial
 
general properties of oerganologametal.ppt
general properties of oerganologametal.pptgeneral properties of oerganologametal.ppt
general properties of oerganologametal.ppt
IqrimaNabilatulhusni
 
Body fluids_tonicity_dehydration_hypovolemia_hypervolemia.pptx
Body fluids_tonicity_dehydration_hypovolemia_hypervolemia.pptxBody fluids_tonicity_dehydration_hypovolemia_hypervolemia.pptx
Body fluids_tonicity_dehydration_hypovolemia_hypervolemia.pptx
muralinath2
 
Orion Air Quality Monitoring Systems - CWS
Orion Air Quality Monitoring Systems - CWSOrion Air Quality Monitoring Systems - CWS
Orion Air Quality Monitoring Systems - CWS
Columbia Weather Systems
 
SCHIZOPHRENIA Disorder/ Brain Disorder.pdf
SCHIZOPHRENIA Disorder/ Brain Disorder.pdfSCHIZOPHRENIA Disorder/ Brain Disorder.pdf
SCHIZOPHRENIA Disorder/ Brain Disorder.pdf
SELF-EXPLANATORY
 
Multi-source connectivity as the driver of solar wind variability in the heli...
Multi-source connectivity as the driver of solar wind variability in the heli...Multi-source connectivity as the driver of solar wind variability in the heli...
Multi-source connectivity as the driver of solar wind variability in the heli...
Sérgio Sacani
 
Earliest Galaxies in the JADES Origins Field: Luminosity Function and Cosmic ...
Earliest Galaxies in the JADES Origins Field: Luminosity Function and Cosmic ...Earliest Galaxies in the JADES Origins Field: Luminosity Function and Cosmic ...
Earliest Galaxies in the JADES Origins Field: Luminosity Function and Cosmic ...
Sérgio Sacani
 
platelets_clotting_biogenesis.clot retractionpptx
platelets_clotting_biogenesis.clot retractionpptxplatelets_clotting_biogenesis.clot retractionpptx
platelets_clotting_biogenesis.clot retractionpptx
muralinath2
 
Hemostasis_importance& clinical significance.pptx
Hemostasis_importance& clinical significance.pptxHemostasis_importance& clinical significance.pptx
Hemostasis_importance& clinical significance.pptx
muralinath2
 
justice-and-fairness-ethics with example
justice-and-fairness-ethics with examplejustice-and-fairness-ethics with example
justice-and-fairness-ethics with example
azzyixes
 
Seminar of U.V. Spectroscopy by SAMIR PANDA
 Seminar of U.V. Spectroscopy by SAMIR PANDA Seminar of U.V. Spectroscopy by SAMIR PANDA
Seminar of U.V. Spectroscopy by SAMIR PANDA
SAMIR PANDA
 
Astronomy Update- Curiosity’s exploration of Mars _ Local Briefs _ leadertele...
Astronomy Update- Curiosity’s exploration of Mars _ Local Briefs _ leadertele...Astronomy Update- Curiosity’s exploration of Mars _ Local Briefs _ leadertele...
Astronomy Update- Curiosity’s exploration of Mars _ Local Briefs _ leadertele...
NathanBaughman3
 
plant biotechnology Lecture note ppt.pptx
plant biotechnology Lecture note ppt.pptxplant biotechnology Lecture note ppt.pptx
plant biotechnology Lecture note ppt.pptx
yusufzako14
 
Richard's aventures in two entangled wonderlands
Richard's aventures in two entangled wonderlandsRichard's aventures in two entangled wonderlands
Richard's aventures in two entangled wonderlands
Richard Gill
 
filosofia boliviana introducción jsjdjd.pptx
filosofia boliviana introducción jsjdjd.pptxfilosofia boliviana introducción jsjdjd.pptx
filosofia boliviana introducción jsjdjd.pptx
IvanMallco1
 
ESR_factors_affect-clinic significance-Pathysiology.pptx
ESR_factors_affect-clinic significance-Pathysiology.pptxESR_factors_affect-clinic significance-Pathysiology.pptx
ESR_factors_affect-clinic significance-Pathysiology.pptx
muralinath2
 
Large scale production of streptomycin.pptx
Large scale production of streptomycin.pptxLarge scale production of streptomycin.pptx
Large scale production of streptomycin.pptx
Cherry
 
extra-chromosomal-inheritance[1].pptx.pdfpdf
extra-chromosomal-inheritance[1].pptx.pdfpdfextra-chromosomal-inheritance[1].pptx.pdfpdf
extra-chromosomal-inheritance[1].pptx.pdfpdf
DiyaBiswas10
 

Recently uploaded (20)

(May 29th, 2024) Advancements in Intravital Microscopy- Insights for Preclini...
(May 29th, 2024) Advancements in Intravital Microscopy- Insights for Preclini...(May 29th, 2024) Advancements in Intravital Microscopy- Insights for Preclini...
(May 29th, 2024) Advancements in Intravital Microscopy- Insights for Preclini...
 
Comparative structure of adrenal gland in vertebrates
Comparative structure of adrenal gland in vertebratesComparative structure of adrenal gland in vertebrates
Comparative structure of adrenal gland in vertebrates
 
Citrus Greening Disease and its Management
Citrus Greening Disease and its ManagementCitrus Greening Disease and its Management
Citrus Greening Disease and its Management
 
general properties of oerganologametal.ppt
general properties of oerganologametal.pptgeneral properties of oerganologametal.ppt
general properties of oerganologametal.ppt
 
Body fluids_tonicity_dehydration_hypovolemia_hypervolemia.pptx
Body fluids_tonicity_dehydration_hypovolemia_hypervolemia.pptxBody fluids_tonicity_dehydration_hypovolemia_hypervolemia.pptx
Body fluids_tonicity_dehydration_hypovolemia_hypervolemia.pptx
 
Orion Air Quality Monitoring Systems - CWS
Orion Air Quality Monitoring Systems - CWSOrion Air Quality Monitoring Systems - CWS
Orion Air Quality Monitoring Systems - CWS
 
SCHIZOPHRENIA Disorder/ Brain Disorder.pdf
SCHIZOPHRENIA Disorder/ Brain Disorder.pdfSCHIZOPHRENIA Disorder/ Brain Disorder.pdf
SCHIZOPHRENIA Disorder/ Brain Disorder.pdf
 
Multi-source connectivity as the driver of solar wind variability in the heli...
Multi-source connectivity as the driver of solar wind variability in the heli...Multi-source connectivity as the driver of solar wind variability in the heli...
Multi-source connectivity as the driver of solar wind variability in the heli...
 
Earliest Galaxies in the JADES Origins Field: Luminosity Function and Cosmic ...
Earliest Galaxies in the JADES Origins Field: Luminosity Function and Cosmic ...Earliest Galaxies in the JADES Origins Field: Luminosity Function and Cosmic ...
Earliest Galaxies in the JADES Origins Field: Luminosity Function and Cosmic ...
 
platelets_clotting_biogenesis.clot retractionpptx
platelets_clotting_biogenesis.clot retractionpptxplatelets_clotting_biogenesis.clot retractionpptx
platelets_clotting_biogenesis.clot retractionpptx
 
Hemostasis_importance& clinical significance.pptx
Hemostasis_importance& clinical significance.pptxHemostasis_importance& clinical significance.pptx
Hemostasis_importance& clinical significance.pptx
 
justice-and-fairness-ethics with example
justice-and-fairness-ethics with examplejustice-and-fairness-ethics with example
justice-and-fairness-ethics with example
 
Seminar of U.V. Spectroscopy by SAMIR PANDA
 Seminar of U.V. Spectroscopy by SAMIR PANDA Seminar of U.V. Spectroscopy by SAMIR PANDA
Seminar of U.V. Spectroscopy by SAMIR PANDA
 
Astronomy Update- Curiosity’s exploration of Mars _ Local Briefs _ leadertele...
Astronomy Update- Curiosity’s exploration of Mars _ Local Briefs _ leadertele...Astronomy Update- Curiosity’s exploration of Mars _ Local Briefs _ leadertele...
Astronomy Update- Curiosity’s exploration of Mars _ Local Briefs _ leadertele...
 
plant biotechnology Lecture note ppt.pptx
plant biotechnology Lecture note ppt.pptxplant biotechnology Lecture note ppt.pptx
plant biotechnology Lecture note ppt.pptx
 
Richard's aventures in two entangled wonderlands
Richard's aventures in two entangled wonderlandsRichard's aventures in two entangled wonderlands
Richard's aventures in two entangled wonderlands
 
filosofia boliviana introducción jsjdjd.pptx
filosofia boliviana introducción jsjdjd.pptxfilosofia boliviana introducción jsjdjd.pptx
filosofia boliviana introducción jsjdjd.pptx
 
ESR_factors_affect-clinic significance-Pathysiology.pptx
ESR_factors_affect-clinic significance-Pathysiology.pptxESR_factors_affect-clinic significance-Pathysiology.pptx
ESR_factors_affect-clinic significance-Pathysiology.pptx
 
Large scale production of streptomycin.pptx
Large scale production of streptomycin.pptxLarge scale production of streptomycin.pptx
Large scale production of streptomycin.pptx
 
extra-chromosomal-inheritance[1].pptx.pdfpdf
extra-chromosomal-inheritance[1].pptx.pdfpdfextra-chromosomal-inheritance[1].pptx.pdfpdf
extra-chromosomal-inheritance[1].pptx.pdfpdf
 

Implement Data Structures with Python Fast using Sparse Matrix

  • 1. I want to become a programming God How to implement any data structures fast in Python? Java C++ Python Matlab Programming Languages… Convex Optimization Competitive Programming Data Structures 1 2 3 4 5 𝑀 = 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 1 0 0 M(final)=𝛿>0( 𝑖=1 ∞ 𝑀 𝑖 ) Transitive Closure Algorithm
  • 2. Sparse Matrix • Any data structures can be represented in dense or sparse matrix • Data structures are • List • Queue • Tree • Graph • Hash Table • … • Sparse Matrix can be easily created in Python
  • 3. Sparse Matrix in Python # Python program to multpliply two # csc matrices using multiply() # Import required libraries import numpy as np from scipy.sparse import csc_matrix # Create first csc matrix A row_A = np.array([0, 0, 1, 2 ]) col_A = np.array([0, 1, 0, 1]) data_A = np.array([4, 3, 8, 9]) cscMatrix_A = csc_matrix((data_A, (row_A, col_A)), shape = (3, 3)) # print first csc matrix print("first csc matrix: n", cscMatrix_A.toarray()) # Create second csc matrix B row_B = np.array([0, 1, 1, 2 ]) col_B = np.array([0, 0, 1, 0]) data_B = np.array([7, 2, 5, 1]) cscMatrix_B = csc_matrix((data_B, (row_B, col_B)), shape = (3, 3)) # print second csc matrix print("second csc matrix:n", cscMatrix_B.toarray()) # Multiply these matrices sparseMatrix_AB = cscMatrix_A.multiply(cscMatrix_B) # print resultant matrix print("Product Sparse Matrix:n", sparseMatrix_AB.toarray())
  • 4. Matlab or Python? • When I started writing codes in Matlab, I felt in love with it • It can do any array operations using indexing syntax, matrix and vector • It eliminates the for loop in array operations • Later I found out that Numpy library in Python can do the same • And even more with more functions in Numpy • Forget about old school Java programming language where data structures are implemented in Objects with methods to operate on the data structures
  • 5. List and Queue • List can be represented in a vectors • Or it can be represented in the sparse matrix M • mij is an element of matrix M • mij is 1 if item j of list is connected to item i • Item i is the next item of item j • Queue is represented in the same approach
  • 6. Tree and Graph • Tree and Graph can be represented by a set of directional edges that connects the items (or vertices) together • The objects of the items are stored in a vector • Example: 1 2 3 4 5 𝑀 = 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 1 0 0 M is a sparse Matrix Item 1: John Item 2: Peter Item 3: Eric Item 4: Ken Item 5: Bob 𝑣 = ′𝐽𝑜ℎ𝑛′ ′𝑃𝑒𝑡𝑒𝑟′ 𝐸𝑟𝑖𝑐′ ′𝐾𝑒𝑛′ ′𝐵𝑜𝑏′ Vector containing object
  • 7. • Navigating through a tree can be represented by matrix multiplication • 𝑀𝑎 = 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 1 0 0 1 0 0 0 0 = 0 1 1 0 0 Immediate children of item 1 Level 1 items of tree 1 2 3 4 5 Level 1
  • 8. • Navigating through a tree can be represented by matrix multiplication • 𝑀𝑀𝑎 = 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 1 0 0 1 0 0 0 0 = 0 0 0 1 1 Immediate children of item 1 Level 2 items of tree 1 2 3 4 5 Level 2
  • 9. • Navigating through a tree can be represented by matrix multiplication • 𝑀𝑀𝑎 = 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 1 0 0 1 0 0 0 0 = 0 0 0 0 0 Immediate children of item 1 Level 3 items of tree No items at level 3 1 2 3 4 5 Level 3
  • 10. • For a graph, the transitive closure is simply the repeated application of matrix multiplication • 𝑀 = 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 1 0 0 1 2 3 4 5
  • 11. • 1 step of transitive closure • 𝑀𝑀 + 𝑀 = 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 1 0 0 + 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 1 0 0 • = 0 0 0 0 0 1 0 0 0 0 1 1 1 0 0 0 0 0 0 1 0 0 1 0 0 1 2 3 4 5
  • 12. • 2 step of transitive closure • 𝑀𝑀𝑀 + 𝑀𝑀 + 𝑀 • = 0 0 0 0 0 1 0 0 0 0 1 1 1 0 0 0 0 0 0 1 0 0 1 0 0 1 2 3 4 5
  • 13. • Transitive closure algorithm in matrix representation is simply • M(final)=𝛿>0( 𝑖=1 ∞ 𝑀𝑖) • Can be written as M(final)=𝛿>0( 𝑀 + 𝐼 𝑖 − 𝐼), where I is an identity matrix • No need to compute until infinity, only need to compute until the matrix converges • It means matrix M raised to the power of infinity (M multiplied to itself infinite times) • And the final matrix after multiplication is thresholded (𝛿>0()) element by element, • Each element is threshold to 1 if it is greater than 0, else it is threshold to 0 • In the example, M(final) = 0 0 0 0 0 1 0 0 0 0 1 1 1 0 0 0 0 0 0 1 0 0 1 0 0
  • 14. Algebraic Representation of Algorithm • I believe a super programming will represent an algorithm in higher order abstract form (algebraic form) • It is much more cleaner and easier to understand • The algorithm can be analyzed symbolically • The properties of the algorithm can be derived algebraically Begin copy the adjacency matrix into another matrix named transMat for any vertex k in the graph, do for each vertex i in the graph, do for each vertex j in the graph, do transMat[i, j] := transMat[i, j] OR (transMat[i, k]) AND transMat[k, j]) done done done Display the transMat End If represented in pseudo codes, it will look very messy. The algebraic matrix notation of the transitive closure algorithm looks better Transitive Closure Algorithm:
  • 15. Convex Optimization representation of Algorithms • I like the convex optimization algorithm, because it is represented so succinctly in algebraic form. And it is so beautiful • Minimize f(x) • Such that g(x)=0 • h(x)>=0 • If all algorithms can be represented in this format, it will be so beautiful • I did some formulation to represent algorithms in convex optimization format • https://www.slideshare.net/SingKuangTan/discrete-markov-random-field-relaxation • https://vixra.org/abs/2112.0151
  • 16. Competitive Programming • I hope my sparse matrix approach to implement data structure will help to speed up programming in competition • Hope that my convex optimization representation of algorithm will also help • I have not take part in programming competition before • Would like to if given a chance • I have did so much programming before and studied my different algorithms…
  • 17. Links to my papers ● https://vixra.org/author/sing_kuang_tan ● Link to my NP vs P paper ● And Discrete Markov Random Field relaxation paper
  • 18. About Me ● My job uses Machine Learning to solve problems ○ Like my posts or slides in LinkedIn, Twitter or Slideshare ○ Follow me on LinkedIn ■ https://www.linkedin.com/in/sing-kuang-tan-b189279/ ○ Follow me on Twitter ■ https://twitter.com/Tan_Sing_Kuang ○ Send me comments through these links ● Look at my Slideshare slides ○ https://www.slideshare.net/SingKuangTan ■ NP vs P Proof using Discrete Finite Automata ■ Use Inductive or Deductive Logic to solve NP vs P? ■ Kung Fu Computer Science, Clique Problem: Step by Step ■ Beyond Shannon, Sipser and Razborov; Solve Clique Problem like an Electronic Engineer ■ A weird Soviet method to partially solve the Perebor Problems ■ 8 trends in Hang Seng Index ■ 4 types of Mathematical Proofs ■ How I prove NP vs P ○ Follow me on Slideshare
  • 19. Share my links ● I am a Small Person with Big Dreams ○ Please help me to repost my links to other platforms so that I can spread my ideas to the rest of the world ● 我人小,但因梦想而伟大。 ○ 请帮我的文件链接传发到其他平台,让我的思想能传遍天下。 ● Comments? Send to singkuangtan@gmail.com ● Link to my paper NP vs P paper ○ https://www.slideshare.net/SingKuangTan/brief-np-vspexplain-249524831 ○ Prove Np not equal P using Markov Random Field and Boolean Algebra Simplification ○ https://vixra.org/abs/2105.0181 ○ Other link ■ https://www.slideshare.net/SingKuangTan