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

DS Lab 12 - Graphs

The document provides information about graphs including their definition, types, modeling problems, basic operations, and graph traversal algorithms like depth-first search and breadth-first search. It also outlines the objectives, concept map, homework and tasks for a lab experiment on graphs.

Uploaded by

Asim Shareef
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views10 pages

DS Lab 12 - Graphs

The document provides information about graphs including their definition, types, modeling problems, basic operations, and graph traversal algorithms like depth-first search and breadth-first search. It also outlines the objectives, concept map, homework and tasks for a lab experiment on graphs.

Uploaded by

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

Department of Computer Science, Faculty of Computing

International Islamic University, Islamabad

Lab Manual for Data


Structures
Lab-12
Graphs
Lab 12: Graphs

Table of Contents
1. Introduction............................................................................................................................................................3
1.1 Types of Graphs.....................................................................................................................................................3
1.2 Modeling Problems................................................................................................................................................4
1.3 Basic Operations....................................................................................................................................................4
2. Activity Time boxing.............................................................................................................................................5
3. Objectives of the experiment.................................................................................................................................5
4. Concept Map..........................................................................................................................................................5
4.1 Representation of Graphs............................................................................................................................................5
4.2 Graph Traversals.........................................................................................................................................................5
5. Homework before Lab.........................................................................................................................................................6
5.1 Problem Solution Modeling........................................................................................................................................6
5.3 Practices from home....................................................................................................................................................6
5.3.1 Task-1......................................................................................................................................................................6
6. Procedure & Tools...............................................................................................................................................................6
6.1 Tools............................................................................................................................................................................6
6.2 Walk through Tasks [Expected time = 20 mins]......................................................................................6
1. Practice Tasks........................................................................................................................................................9
7.1 Practice Task 1 [Expected time = 30 mins].............................................................................................9
7.2 Practice Task 2 [Expected time = 40 mins]............................................................................................9
7.3 Out comes....................................................................................................................................................................9
8. Evaluation Task (Unseen) [Expected time = 60 mins for two tasks]...............................................................9
9. Evaluation criteria...............................................................................................................................................................9
10. Further Readings.............................................................................................................................................................10
10.1 Web sites related to C++ tutorials about binary search trees using arrays.............................................................10

Department of Computer Science Page 2


IIU, Islamabad
Lab 12: AVL Trees

Lab 12: Graphs

1. Introduction
A graph is a data structure that contains of a set of vertices and a set of edges which connect
pairs of the vertices.

 A vertex (or node) can be connected to any number of other vertices using edges.
 An edge may be bidirectional or directed (one-way).
 An edge may have a weight on it that indicates a cost for traveling over that edge in the
graph.
 Unlike trees, graphs can contain cycles, In fact a tree is an acyclic graph

OR

A graph is a collection of nodes (vertices) and edges. Edges connect pairs of nodes, indicating a
relationship between them. Graphs can be directed (edges have a specific direction) or
undirected.

Applications: computer networks, transportation systems, social networks

1.1 Types of Graphs

Directed Graph

A directed graph G is a pair of sets (V, E) where V is a set of nodes and E is a set of ordered
node pairs.

Undirected Graph

Department of Computer Science Page 3


IIU, Islamabad
Lab 12: AVL Trees

An undirected graph G is a pair of sets (V, E) where V is a set of nodes and E is a set of node
pairs

1.2 Modeling Problems

A lot of different problems can be modeled with graphs.

 Road Networks
 Social Networks
 Molecules

1.3 Basic Operations

Accessing information
 Given a node or edge, access its information, e.g. edge weight, distance from other node,
etc.

Navigation
 Given a node, access its outgoing edges.
 This operation lies in the heart of most graph algorithms.
 Sometimes we would also like easy access to the incoming edges of a node.

Edge Queries
 Given 2 nodes (u, v) we would like to know if such an edge exists in the graph.
 Sometimes we would like to easily find the opposite edge (v, u) of a directed edge (u, v).

Construct, convert and output


 Convert the representation to the most natural form for the problem that we are trying to
solve.

Department of Computer Science Page 4


IIU, Islamabad
Lab 12: AVL Trees

Update
 Add and remove nodes or edges, change the information of node or edge, etc.

2. Activity Time boxing


Table 1: Activity Time Boxing

Task No. Activity Name Activity time Total Time


5.1 Design Evaluation 20mins 20mins
6.2 Walk through tasks 20mins 20mins
7 Practice tasks 30 mins for task 1 and 40 mins 70mins
for task 2
9 Evaluation Task 60mins for all assigned tasks 60mins

3. Objectives of the experiment


 To understand implementation of graphs in C++.

4. Concept Map
This concept map will help students to understand the main concepts of topic covered in
lab.

4.1 Representation of Graphs


Adjacency Matrix:
 A 2D array where each element matrix[i][j] represents the connection between node i and
node j.
 Efficient for dense graphs with many connections.
Adjacency List:
 A collection of linked lists where each list represents the neighbors of a node.
 Efficient for sparse graphs with fewer connections.

4.2 Graph Traversals

Depth-First Search (DFS):


 Recursive approach where a node is visited and then its unvisited neighbors are explored.
 Backtracking is used to explore as far as possible along each branch.
Breadth-First Search (BFS):
 Iterative approach using a queue to explore all neighbors at the current depth before
moving on to the next level.
 Useful for finding the shortest path in an unweighted graph.

Department of Computer Science Page 5


IIU, Islamabad
Lab 12: AVL Trees

5. Homework before Lab


This homework will help students to study the concepts of topic before start of lab.
5.1 Problem Solution Modeling
After studying the introduction and concept map sections you should be ready to provide
the solution of following problems. Design the solutions of the problems in C++ and
bring your code to lab so that lab instructor should assess and grade it.

5.3 Practices from home


5.3.1 Task-1
Distinguish between recursive and iterative traversal in binary trees.

6. Procedure & Tools


This section provides information about tools and programming procedures used for the
lab.
6.1 Tools
Microsoft Visual Studio 2017 with Visual C++ compiler configured.

6.2 Walk through Tasks [Expected


time = 20 mins]
Following screens in figure 3 represent the implementation of binary tree using array in
Microsoft Visual Studio 2017 for in order traversal. You are required to type, debug and execute
this program in Microsoft Visual Studio 2017.

Undirected graph using an adjacency list in C++:

Department of Computer Science Page 6


IIU, Islamabad
Lab 12: AVL Trees

Department of Computer Science Page 7


IIU, Islamabad
Lab 12: AVL Trees

Department of Computer Science Page 8


IIU, Islamabad
Lab 12: AVL Trees

1. Practice Tasks
This section will provide information about the practice tasks which are required to be performed
in lab session. Design solutions of problems discussed in each task and place solution code in a
folder specified by your lab instructor.

7.1 Practice Task 1 [Expected


time = 30 mins]
Create a C++ program to determine if a given undirected graph is cyclic or acyclic. Implement
the program to take user input for the number of vertices and edges, along with the connections
between vertices. Use any graph representation (adjacency matrix or adjacency list) and output
whether the graph contains a cycle or is acyclic.

7.2 Practice Task 2 [Expected


time = 40 mins]
Write a C++ program that finds the shortest path between two nodes in a weighted directed
graph. The program should take user input for the number of vertices, edges, and the weights of
the edges. Implement Dijkstra's algorithm to find the shortest path from a specified source node
to a target node. Display the shortest path and its total weight.

7.3 Out comes


After completing this lab, student will be able to understand and develop programs related to
operate general binary trees using linked list in C++ using Microsoft Visual Studio 2017
environment. Further students will be able to implement iterative traversal operations like pre
order, in order and post order traversal on general binary trees in C++.

8. Evaluation Task (Unseen)


[Expected time = 60 mins for two tasks]

The lab instructor will give you unseen task depending upon the progress of the class.

9. Evaluation criteria
The evaluation criteria for this lab will be based on the completion of the following tasks. Each
task is assigned the marks percentage which will be evaluated by the instructor in the lab whether
the student has finished the complete/partial task(s).

Table 2: Evaluation of the Lab


Sr. No. Task No Description Marks
1 4 Problem Modeling 20
2 6 Procedures and Tools 10
3 7,8 Practice tasks and Testing 35
4 8.1 Evaluation Tasks (Unseen) 20
5 Comments 5
6 Good Programming Practices 10
Department of Computer Science Page 9
IIU, Islamabad
Lab 12: AVL Trees

10. Further Readings


10.1 Web sites related to C++ tutorials about binary search trees
using arrays
1. https://d-michail.github.io/assets/teaching/data-structures/060_Graphs.en.pdf/
2. https://www.kosbie.net/cmu/fall-18/15-110/notes/trees-and-graphs.pdf

Department of Computer Science Page 10


IIU, Islamabad

You might also like