SlideShare a Scribd company logo
Basic Data Structures in Prolog
Lists and Graphs
Overview
Lists
List Operations
Graphs
Graph Operations
Lists
What are the classic properties of lists?
 Easy to insert and delete at end (free pointer)
 Harder to change in the middle
 O(n) to search unless ordered
 Variable size
Prolog Lists
The major complex data structure
Can contain atoms, variables, etc.
Have 2 parts, a Head and a Tail
 Head is the first element in a list
 Tail is everything else
 [Head|Tail]
The empty list [] is a list
Every list has an empty list at the end
Getting a Feel for Lists
head( [H|T], H ).
tail( [H|T], T ).
Every nontrivial list operation involves
recursion
Member
member( H, [H|T] ).
member( H, [_|T] ):-
member( H, T ).
Append
append( H, [], [H] ).
append( X, [H|T], [H|T2] ):-
append( X, T, T2 ).
Delete
delete( H, [H|T], T ).
delete( X, [H|T], [H|T2] ):-
delete( X, T, T2 ).
Delete can test membership!
Length
length( [], 0 ).
length( [H|T], X+1 ):-
length( T, X ).
Concat
concat( H, [], H ).
concat( [H|T], [H2|T2], [H|T3] ):-
append( H2, T, H3 ),
concat( H3, T2, T3 ).
concat2( [], H, H ).
concat2( [H|T], L2, [H|T2] ):-
concat2( T, L2, T2 ).
Sort
sort1( [], [] ).
sort1( L1, L2 ):-
permute( L1, L2 ),
ordered( L2 ).
Permute
permute( [], [] ).
permute( L, [H|T] ):-
delete( H, L, L2 ),
permute( L2, T ).
Ordered
ordered( [] ).
ordered( [X|[]] ).
ordered( [H|[H2|T]] ):-
H < H2,
ordered( [H2|T] ).
Flatten - Double Recursion
flatten( [], [] ).
flatten( X, [X] ):-
atomic( X ),
X == [].
flatten( [H|T], L ):-
flatten( H, H2 ),
flatten( T, T2 ),
concat( H2, T2, L ).
Graphs
Representations
 Adjacency matrix
A
E
D
C
B
a b c d e
a 0 1 0 0 1
b 0 0 1 1 0
c 0 0 0 0 0
d 0 0 0 0 0
e 0 0 0 0 0
Graphs
Representations
 Adjacency list
 a : b,e
 b : c,d
 c :
 d :
 e :
A
E
D
C
B
Graphs in Prolog
Using terms
 arc( a, b ).
 arc( a, e ).
 arc( b, c ).
 arc( b, d ).
Technically, a graph is a set of nodes
and edges, but you may not care...
A
E
D
C
B
Graphs in Prolog
Are a and d connected?
 connected( X, Y ):-
 arc( X, Y ).
 connected( X, Z ):-
 arc( X, Y ),
 connected( Y, Z ).
A
E
D
C
B
Depth First Search
Return path to goal state
 dfs( N, [] ):-
 goal( N ).
 dfs( N, [H|T] ):-
 connected( N, H ),
 dfs( H, T ).
A
E
D
C
B
Graphs in Prolog
What if you want a graph/tree to be an
argument to a predicate?
graph([nodes],[edges])
 graph([a,b,c,d,e],[arc(a,b),arc(a,e),arc(b,c)
,arc(b,d)]
graph( t(node, [edgenodes]), ... )
 graph( t(a, [b,e]), t(b, [c,d]), t(c, []), t(d,
[]), t(e,[]) )

More Related Content

Similar to PrologListsGraphs.ppt

python_avw - Unit-03.pdf
python_avw - Unit-03.pdfpython_avw - Unit-03.pdf
python_avw - Unit-03.pdf
AshaWankar1
 
Week 4
Week 4Week 4
Week 4
a_akhavan
 
Python lecture 07
Python lecture 07Python lecture 07
Python lecture 07
Tanwir Zaman
 
11 library
11 library11 library
11 library
Siham Rim Boudaoud
 
List and Dictionary in python
List and Dictionary in pythonList and Dictionary in python
List and Dictionary in python
Sangita Panchal
 
Monoids
MonoidsMonoids
Monoids
saulius_vl
 
Succinct Data Structure for Analyzing Document Collection
Succinct Data Structure for Analyzing Document CollectionSuccinct Data Structure for Analyzing Document Collection
Succinct Data Structure for Analyzing Document Collection
Preferred Networks
 
Zippers
ZippersZippers
Zippers
David Overton
 
Lecture-5.pdf
Lecture-5.pdfLecture-5.pdf
Lecture-5.pdf
Bhavya103897
 
Levitan Centenary Conference Talk, June 27 2014
Levitan Centenary Conference Talk, June 27 2014Levitan Centenary Conference Talk, June 27 2014
Levitan Centenary Conference Talk, June 27 2014
Nikita V. Artamonov
 
Existence of Solutions of Fractional Neutral Integrodifferential Equations wi...
Existence of Solutions of Fractional Neutral Integrodifferential Equations wi...Existence of Solutions of Fractional Neutral Integrodifferential Equations wi...
Existence of Solutions of Fractional Neutral Integrodifferential Equations wi...
inventionjournals
 
An Introduction to Functional Programming using Haskell
An Introduction to Functional Programming using HaskellAn Introduction to Functional Programming using Haskell
An Introduction to Functional Programming using Haskell
Michel Rijnders
 
Application of Module Structure of Algebra in Homomorphic Signal Processing
Application of Module Structure of Algebra in Homomorphic Signal ProcessingApplication of Module Structure of Algebra in Homomorphic Signal Processing
Application of Module Structure of Algebra in Homomorphic Signal Processing
ijsrd.com
 
Modular Module Systems
Modular Module SystemsModular Module Systems
Modular Module Systems
league
 
purrr.pdf
purrr.pdfpurrr.pdf
purrr.pdf
Mateus S. Xavier
 
Tree distance algorithm
Tree distance algorithmTree distance algorithm
Tree distance algorithm
Trector Rancor
 
Ijmet 10 01_046
Ijmet 10 01_046Ijmet 10 01_046
Ijmet 10 01_046
IAEME Publication
 
Spectral Continuity: (p, r) - Α P And (p, k) - Q
Spectral Continuity: (p, r) - Α P And (p, k) - QSpectral Continuity: (p, r) - Α P And (p, k) - Q
Spectral Continuity: (p, r) - Α P And (p, k) - Q
IOSR Journals
 
Array
ArrayArray
All about a cosets and Lagrange's theorem
All about a cosets and Lagrange's theoremAll about a cosets and Lagrange's theorem
All about a cosets and Lagrange's theorem
AlmaPalceAbarollo
 

Similar to PrologListsGraphs.ppt (20)

python_avw - Unit-03.pdf
python_avw - Unit-03.pdfpython_avw - Unit-03.pdf
python_avw - Unit-03.pdf
 
Week 4
Week 4Week 4
Week 4
 
Python lecture 07
Python lecture 07Python lecture 07
Python lecture 07
 
11 library
11 library11 library
11 library
 
List and Dictionary in python
List and Dictionary in pythonList and Dictionary in python
List and Dictionary in python
 
Monoids
MonoidsMonoids
Monoids
 
Succinct Data Structure for Analyzing Document Collection
Succinct Data Structure for Analyzing Document CollectionSuccinct Data Structure for Analyzing Document Collection
Succinct Data Structure for Analyzing Document Collection
 
Zippers
ZippersZippers
Zippers
 
Lecture-5.pdf
Lecture-5.pdfLecture-5.pdf
Lecture-5.pdf
 
Levitan Centenary Conference Talk, June 27 2014
Levitan Centenary Conference Talk, June 27 2014Levitan Centenary Conference Talk, June 27 2014
Levitan Centenary Conference Talk, June 27 2014
 
Existence of Solutions of Fractional Neutral Integrodifferential Equations wi...
Existence of Solutions of Fractional Neutral Integrodifferential Equations wi...Existence of Solutions of Fractional Neutral Integrodifferential Equations wi...
Existence of Solutions of Fractional Neutral Integrodifferential Equations wi...
 
An Introduction to Functional Programming using Haskell
An Introduction to Functional Programming using HaskellAn Introduction to Functional Programming using Haskell
An Introduction to Functional Programming using Haskell
 
Application of Module Structure of Algebra in Homomorphic Signal Processing
Application of Module Structure of Algebra in Homomorphic Signal ProcessingApplication of Module Structure of Algebra in Homomorphic Signal Processing
Application of Module Structure of Algebra in Homomorphic Signal Processing
 
Modular Module Systems
Modular Module SystemsModular Module Systems
Modular Module Systems
 
purrr.pdf
purrr.pdfpurrr.pdf
purrr.pdf
 
Tree distance algorithm
Tree distance algorithmTree distance algorithm
Tree distance algorithm
 
Ijmet 10 01_046
Ijmet 10 01_046Ijmet 10 01_046
Ijmet 10 01_046
 
Spectral Continuity: (p, r) - Α P And (p, k) - Q
Spectral Continuity: (p, r) - Α P And (p, k) - QSpectral Continuity: (p, r) - Α P And (p, k) - Q
Spectral Continuity: (p, r) - Α P And (p, k) - Q
 
Array
ArrayArray
Array
 
All about a cosets and Lagrange's theorem
All about a cosets and Lagrange's theoremAll about a cosets and Lagrange's theorem
All about a cosets and Lagrange's theorem
 

Recently uploaded

TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEMTIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
HODECEDSIET
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
SUTEJAS
 
CSM Cloud Service Management Presentarion
CSM Cloud Service Management PresentarionCSM Cloud Service Management Presentarion
CSM Cloud Service Management Presentarion
rpskprasana
 
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
University of Maribor
 
Textile Chemical Processing and Dyeing.pdf
Textile Chemical Processing and Dyeing.pdfTextile Chemical Processing and Dyeing.pdf
Textile Chemical Processing and Dyeing.pdf
NazakatAliKhoso2
 
Engine Lubrication performance System.pdf
Engine Lubrication performance System.pdfEngine Lubrication performance System.pdf
Engine Lubrication performance System.pdf
mamamaam477
 
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMSA SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
IJNSA Journal
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
IJECEIAES
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
MDSABBIROJJAMANPAYEL
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
IJECEIAES
 
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdfIron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
RadiNasr
 
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
JamalHussainArman
 
Heat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation pptHeat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation ppt
mamunhossenbd75
 
Engineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdfEngineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdf
abbyasa1014
 
New techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdfNew techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdf
wisnuprabawa3
 
ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024
Rahul
 
Manufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptxManufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptx
Madan Karki
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
NidhalKahouli2
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
171ticu
 
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
171ticu
 

Recently uploaded (20)

TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEMTIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
 
CSM Cloud Service Management Presentarion
CSM Cloud Service Management PresentarionCSM Cloud Service Management Presentarion
CSM Cloud Service Management Presentarion
 
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
 
Textile Chemical Processing and Dyeing.pdf
Textile Chemical Processing and Dyeing.pdfTextile Chemical Processing and Dyeing.pdf
Textile Chemical Processing and Dyeing.pdf
 
Engine Lubrication performance System.pdf
Engine Lubrication performance System.pdfEngine Lubrication performance System.pdf
Engine Lubrication performance System.pdf
 
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMSA SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
 
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdfIron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
 
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
 
Heat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation pptHeat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation ppt
 
Engineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdfEngineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdf
 
New techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdfNew techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdf
 
ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024
 
Manufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptxManufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptx
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
 
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
 

PrologListsGraphs.ppt