Docsity

Algorithm graph lab manual, Lecture notes of Algorithms and Programming

Toggle sidebar
bg1
Graph: A graph is a set of nodes (vertices) and edges. The node that holds data is called vertex and the line
connecting two vertices is called edge. If G denotes a graph, G= (V, E) then V denotes set of vertices and E
denotes set of edges.
Weighted Graph: If the value (cost) of each edge is given, the graph is called weighted graph.
Path: A path is a sequence of vertices where each pair of successive vertices is connected by an edge.
Connected Graph: A graph is called connected if there is a path between each pair of vertices
Cycle: A cycle is a path where first and last vertices are the same.
GRAPH REPRESENTATION
There are several possible ways to represent a graph inside the computer. We will discuss two of them:
Adjacency matrix and Adjacency list.
Adjacency matrix
Each cell aij of an adjacency matrix contains 1, if there is an edge between i-th and j-th vertices, and 0
otherwise. Before discussing the advantages and disadvantages of this kind of representation, let us see an
example.
Creating Matrix
Matrix in this example will be a dynamic two-dimensional array. We will use a
pointer-to-pointer-to-integer type structure.
Matrix Types
int *p[row]
int **p
LAB MANUAL 06
AMERICAN INTERNATIONAL UNIVERSITY-BANGLADESH (AIUB)
FACULTY OF SCIENCE & INFORMATION TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE
TITLE| Graph Representations and Traversals
PREREQUISITE | Knowledge of Arrays| Linked lists | STL
THEORY | BASICS OF GRAPH
OBJECTIVE | Learn Graph Representation, Implementation & Graph Search Strategies
Fall 2015-2016
CSC2211 Algorithms |
row
2
col
2
p
p[0]
p [1]
pf3
pf4
pf5
pf8

Partial preview of the text

Download Algorithm graph lab manual and more Lecture notes Algorithms and Programming in PDF only on Docsity!

Graph: A graph is a set of nodes (vertices) and edges. The node that holds data is called vertex and the line

connecting two vertices is called edge. If G denotes a graph, G= (V, E) then V denotes set of vertices and E

denotes set of edges.

Weighted Graph : If the value (cost) of each edge is given, the graph is called weighted graph.

Path: A path is a sequence of vertices where each pair of successive vertices is connected by an edge.

Connected Graph: A graph is called connected if there is a path between each pair of vertices

Cycle: A cycle is a path where first and last vertices are the same.

GRAPH REPRESENTATION

There are several possible ways to represent a graph inside the computer. We will discuss two of them:

Adjacency matrix and Adjacency list.

Adjacency matrix

Each cell aij of an adjacency matrix contains 1 , if there is an edge between i-th and j-th vertices, and 0

otherwise. Before discussing the advantages and disadvantages of this kind of representation, let us see an

example.

Creating Matrix

Matrix in this example will be a dynamic two-dimensional array. We will use a

pointer-to-pointer-to-integer type structure.

Matrix Types

int *p[row]

int **p

LAB MANUAL 06

AMERICAN INTERNATIONAL UNIVERSITY-BANGLADESH (AIUB)

FACULTY OF SCIENCE & INFORMATION TECHNOLOGY

DEPARTMENT OF COMPUTER SCIENCE

TITLE | Graph Representations and Traversals

PREREQUISITE | Knowledge of Arrays| Linked lists | STL

THEORY | BASICS OF GRAPH

OBJECTIVE | Learn Graph Representation, Implementation & Graph Search Strategies

Fall 2015- 2016 CSC2211 Algorithms |

row 2

col 2

p

p[0]

p [1]

Implementing Matrix

// dimensions, row × column

int row, col;

// pointer to a pointer to a integer

int **Graph;

Allocate the array Graph and the Graph[i] arrays:

Graph = new int*[row];

for (int i = 0; i < row; i++)

Graph[i] = new int[col];

Code snippets

For reasons of simplicity, code snippets for adjacency matrix have been given below.

Notice, that it is an implementation for undirected graphs.

// initialized to all 0 entries

for (int i = 0; i < row; i++)

{ for (int j = 0; j < col; j++)

Graph[i][j] = 0;

Graph::Graph(int vertexCount) { this->vertexCount = vertexCount; adjacencyMatrix = new bool*[vertexCount]; // if weighted //graph then also change here as well for (int i = 0; i < vertexCount; i++) { adjacencyMatrix[i] = new bool[vertexCount]; //for weighted, //graph also change here for (int j = 0; j < vertexCount; j++) adjacencyMatrix[i][j] = false; // if no weight then place - 1 } } Graph::~Graph() { for (int i = 0; i < vertexCount; i++) delete[] adjacencyMatrix[i]; delete[] adjacencyMatrix; } #include using namespace std; class Graph { private: bool ** adjacencyMatrix; // use int type for weighted graph int vertexCount; public: Graph(int vertexCount); ~Graph(); void addEdge(int i, int j); void removeEdge(int i, int j); bool isEdge(int i, int j); void display(); }; void Graph::addEdge(int i, int j) { if (i >= 0 && i < vertexCount && j >= 0 && j < vertexCount) { adjacencyMatrix[i][j] = true; adjacencyMatrix[j][i] = true; } } void Graph::removeEdge(int i, int j) { if (i >= 0 && i < vertexCount && j >= 0 && j < vertexCount) { adjacencyMatrix[i][j] = false; adjacencyMatrix[j][i] = false; } }

Task 3: Add additional member function called findAdjacencyVertices(int vertex), which displays adjacency

nodes/vertices of that vertex.

Task 4: Add additional member function called findDegree(int vertex), which displays the number of vertices that are

connected of that vertex.

Task5: Add another method named findDisconnectedNodes(), which will displays all the disconnected nodes of the

graph.

Consider the below weighted graph:

Note: Consider a as 0 node for convenient.

Task 1: Develop a new class named WeightedGraph and do the followings

1. Display the graph with weights

2. Find adjacency nodes of a vertex

3. Find in-degree and out-degree of a vertex

4. Change weight of any two nodes connection.

Task:

Try to implement the undirected graph using link list. You can take help with others or google but don’t copy &

paste.

GRAPH SEARCH METHODS

There are two principal methods for graph traversal, which are as follows:

1. Breadth First Search(BFS)

2. Depth First Search(DFS)

BREADTH FIRST SEARCH (BFS)

Breadth-first search is one of the simplest algorithms for searching a graph and the archetype for many

important graph algorithms. Prim’s minimum-spanning tree algorithm and Dijkstra’s single-source shortest-

paths algorithm use ideas similar to those in breadth-first search. Given a graph G= (V, E) and a distinguished

source vertex s, breadth-first search systematically explores the edges of G to “discover” every vertex that is

reachable from s. It computes the distance (smallest number of edges) from s to each reachable vertex. It also

produces a “breadth-first tree” with root s that contains all reachable vertices. For any vertex reachable from

s, the simple path in the breadth-first tree from s to v corresponds to a “shortest path” from s to v in G, that is,

a path containing the smallest number of edges. The algorithm works on both directed and undirected graphs.

The pseudo-code for Breadth-first search (BFS) is given below:

Initially all vertices are white (unvisited). DFS starts in arbitrary vertex and runs as follows:

1. Mark vertex u as gray (visited).

2. For each edge (u, v) , where u is white, run depth-first search for u recursively.

3. Mark vertex u as black and backtrack to the parent.

Task 3

Use the following code and above given graph to perform depth first search on it. Print the DFS visited node order on the

output screen.

Task 4

Show BFS generated nodes for the same graph on the other side of this page. Also write your own bit of code

to do BFS traversal on the same graph.

1. The node that holds data is called and the line connecting two vertices is called edge.

2. If the value of each edge is given, the graph is called graph.

3. A graph is stored in the computer memory using the adjacency or adjacency.

4. There are two principal methods for graph traversal, which are and.

5. A is a sequence of vertices where each pair of successive vertices is connected by an edge.

EXERCISE

void Graph::DFS() { VertexState *state = new VertexState[vertexCount]; for (int i = 0; i < vertexCount; i++) state[i] = White; runDFS(0, state); delete [] state; } void Graph::runDFS(int u, VertexState state[]) { state[u] = Gray; for (int v = 0; v < vertexCount; v++) if (isEdge(u, v) && state[v] == White) runDFS(v, state); state[u] = Black; cout<<u<<","; }