0% found this document useful (0 votes)
28 views10 pages

Practical 08

This is Ai lab manual part 8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views10 pages

Practical 08

This is Ai lab manual part 8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Department of: Subject of:

Information Technology Artificial Intelligence

Quaid-e-Awam University of Year 4TH Semester 8TH


Engineering, Sciences &
Technology Batch 19IT Duration 03
Hours
Nawabshah

PRACTICAL 08
TO DEMONSTRATE & EXECUTE SEARCHING PROBLEMS IN GRAPHS

----------------------------------------------------------
OBJECTIVES

• Fundamentals of graphs
• Applications of graphs
• Creation of graphs using dictionaries
• Generating edges
• Find isolated nodes
• Finding path between two vertex/ node
• Finding all paths in graphs
• Finding shortest path between nodes
• Determine cycles in graphs
• Add an edge

REQUIREMENTS

• PC with windows
• Anaconda environment/Online python compiler
8.1 FUNDAMENTALS OF GRAPHS

• Vertex
It is a point where multiple lines meet. It’s also known as the node. A
vertex is generally denoted by an alphabet as shown below.

• Edge
It is a line that connects two vertices.

• Graph
Graph is a combination of vertices (nodes) and edges.
G = (V, E) where V represents the set of all vertices and E represents the set of all
edges of the graph.

• Directed Graph
Directed graphs have edges with direction. The edges indicate a one-way
relationship, in that each edge can only be traversed in a single direction.
• Un-Directed Graph
Un-directed graph have edges that do not have a direction. The edges indicate a
twoway relationship, in that each edge can be traversed in both directions.

8.2 Applications of Graphs


• Many practical problems can be represented by graphs.
• They are often used to model problems or situations in physics, biology, and
psychology and above all in computer science.
• In computer science, graphs are used to represent:
✓ Networks of communication,
✓ Data organization,
✓ Computational devices,
✓ The flow of computation,
✓ They are used to represent the data organization like the file system of an
operating system
✓ Communication networks.

8.3 Generate Graph Using Python


Python has no built-in data type or class for graphs, but it is easy to implement them in
Python.
One data type is ideal for representing graphs in Python, i.e. dictionaries (A dictionary is a
collection which is unordered, changeable and indexed. In Python dictionaries are written
with curly brackets, and they have keys and values.).
EXAMPLE 1
Implement the following graph in python.

CODE

OUTPUT

EXAMPLE 2
Implement the graph of example 1 by creating a function of edges.
8.4 Isolated Node
Any node which is not associated with any edge is called isolated node.

ISOLATED NODE

8.4.1 Finding Isolated Nodes


As we can see, there is no edge containing the node "f". "F" is an isolated node of our graph.
The following Python function calculates the isolated nodes of a given graph:

8.5 Finding All Paths between Two Nodes/Vertices


Let's write a simple function to determine a path between two nodes. It takes a graph and the
start and end nodes as arguments. It will return a list of nodes (including the start and end
nodes) comprising the path. When no path can be found, it returns none.
8.6 Finding Shortest Path between Two Nodes/Vertices
The shortest path is a path between two vertices (or nodes) in a graph such that the sum
of the weights of its constituent edges is minimized.
8.7 Adding Edge
8.8 Cycles in Graph
A path from a vertex to itself is called cycle. A graph is cyclic if it has cycle in it otherwise it is
acyclic.
EXERCISE

1. Define the following terms:


• Regular Graph
• Null Graph
• Trivial Graph
• Simple Graph
• Connected Graph
• Disconnected Graph
• Complete Graph
• Cyclic Graph
• Degree of vertex
• Loop
• Parallel Edges

2. Consider the following graph:

a) Find isolated nodes.


b) Find path between two vertex/ node 1 and 7.
c) Find all paths in graphs.
d) Find shortest path between nodes 1 and 7.
e) Determine cycles in graphs.
f) Add an edge named 9.
3. Consider the following graph:

a. Find isolated nodes.


b. Find path between two vertex/node B and A.
c. Find all paths in graphs.
d. Find shortest path between nodes B and A.
e. Determine cycles in graphs.
f. Add an edge named K.

4. Consider the following graph:

a. Find isolated nodes.


b. Find path between two vertex/node Thomas’ Farm and Library.
c. Find all paths in graph.
d. Finding shortest path between nodes Thomas’ Farm and
Library.
e. Determine cycles in graphs.
f. Add an edge named John’s House.

You might also like