0% found this document useful (0 votes)
94 views

Chapter 9 Data Structure Slide

The document discusses graphs as a data structure and common graph algorithms. It defines what graphs are, the terminology used in graphs, different representations of graphs, and algorithms for searching graphs like depth-first search and breadth-first search. It also covers the single-source shortest path problem and algorithms to solve it like Dijkstra's and Bellman-Ford.

Uploaded by

gaby
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views

Chapter 9 Data Structure Slide

The document discusses graphs as a data structure and common graph algorithms. It defines what graphs are, the terminology used in graphs, different representations of graphs, and algorithms for searching graphs like depth-first search and breadth-first search. It also covers the single-source shortest path problem and algorithms to solve it like Dijkstra's and Bellman-Ford.

Uploaded by

gaby
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 38

Graphs

Data Structures

Dr. Fares saab

What is a graph?
A data

structure that consists of a set of


nodes (vertices) and a set of edges that
relate the nodes to each other

The

set of edges describes relationships


among the vertices

Dr. Fares saab

Formal definition of graphs

A graph G is defined as follows:


G=(V,E)
V(G): a finite, nonempty set of vertices
E(G): a set of edges (pairs of vertices)

Dr. Fares saab

Directed vs. undirected


graphs

When the edges in a graph have no direction, the graph is called


undirected

Dr. Fares saab

Directed vs. undirected


graphs (cont.)

When the edges in a graph have a direction, the graph is called


directed (or digraph)

Warning: if the graph is


directed, the order of the
vertices in each edge is
important !!
Dr. Fares saab

E(Graph2) = {(1,3) (3,1) (5,9) (9,11) (5,7)

Trees vs graphs

Trees are special cases of graphs!!

Dr. Fares saab

Graph terminology

Adjacent nodes: two nodes are adjacent if they are connected by an


edge

is adjacent
7
Path: a sequence of vertices that connect 5two
nodes in atograph
7 is adjacent from 5

Complete graph: a graph in which every vertex is directly connected


to every other vertex

Dr. Fares saab

Graph terminology (cont.)

What is the number of edges in a


complete directed graph with N vertices?
N * (N-1)

O( N )

Dr. Fares saab

Graph terminology (cont.)


Weighted

graph: a graph in which each edge


carries a value

Dr. Fares saab

Graph implementation

Array-based implementation

A 1D array is used to represent the vertices

A 2D array (adjacency matrix) is used to represent the edges

Dr. Fares saab

10

Array-based implementation

Dr. Fares saab

11

Graph implementation (cont.)

Linked-list implementation

1D array is used to represent the vertices

list is used for each vertex v which contains


the vertices which are adjacent from v
(adjacency list)

Dr. Fares saab

12

Linked-list implementation

Dr. Fares saab

13

Adjacency matrix vs.


adjacency list representation

Adjacency matrix

Connectivity between two vertices can be tested quickly

Adjacency list

Dr. Fares saab

Vertices adjacent to another vertex can be found quickly

14

Graph specification based on


adjacency matrix representation
const int NULL_EDGE = 0;

private:
int numVertices;
int maxVertices;
VertexType* vertices;
int **edges;
bool* marks;
};

template<class VertexType>
class GraphType {
public:
GraphType(int);
~GraphType();
void MakeEmpty();
bool IsEmpty() const;
bool IsFull() const;
void AddVertex(VertexType);
void AddEdge(VertexType, VertexType, int);
int WeightIs(VertexType, VertexType);
void GetToVertices(VertexType, QueType<VertexType>&);
void ClearMarks();
void MarkVertex(VertexType);
bool
(continues)
Dr. Fares
saab IsMarked(VertexType) const;
15

template<class VertexType>
GraphType<VertexType>::GraphType(int maxV)
{
numVertices = 0;
maxVertices = maxV;
vertices = new VertexType[maxV];
edges = new int[maxV];
for(int i = 0; i < maxV; i++)
edges[i] = new int[maxV];
marks = new bool[maxV];
}
template<class VertexType>
GraphType<VertexType>::~GraphType()
{
delete [] vertices;
for(int i = 0; i < maxVertices; i++)
delete [] edges[i];
delete [] edges;
delete [] marks;
}
Dr. Fares saab

(continues)
16

void GraphType<VertexType>::AddVertex(VertexType vertex)


{

vertices[numVertices] = vertex;
for(int index = 0; index < numVertices; index++) {
edges[numVertices][index] = NULL_EDGE;
edges[index][numVertices] = NULL_EDGE;
}

numVertices++;
}

template<class VertexType>
void GraphType<VertexType>::AddEdge(VertexType fromVertex,
VertexType toVertex, int weight)
{

int row;
int column;
row = IndexIs(vertices, fromVertex);
col = IndexIs(vertices, toVertex);
edges[row][col] = weight;
}
Dr. Fares saab

(continues)
17

template<class VertexType>
int GraphType<VertexType>::WeightIs(VertexType fromVertex,
VertexType toVertex)
{
int row;
int column;
row = IndexIs(vertices, fromVertex);
col = IndexIs(vertices, toVertex);
return edges[row][col];
}

Dr. Fares saab

18

Graphs as ADTs
ADT graph operations

Test whether graph is empty.

Get number of vertices in a graph.

Get number of edges in a graph.

See whether edge exists between two given vertices.

Insert vertex in graph whose vertices have distinct values that differ from new
vertexs value.

Dr. Fares saab

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, 2013

Graphs as ADTs
ADT graph operations, ctd.

Insert edge between two given vertices in graph.

Remove specified vertex from graph and any edges between the vertex and other
vertices.

Remove edge between two vertices in graph.

Retrieve from graph vertex that contains given value.

Dr. Fares saab

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, 2013

Graph searching

Problem: find a path between two nodes of the graph (e.g., Austin
and Washington)

Methods: Depth-First-Search (DFS) or Breadth-First-Search (BFS)

Dr. Fares saab

21

Depth-First-Search (DFS)

What is the idea behind DFS?

Travel as far as you can down a path

Back up as little as possible when you reach a "dead end"


(i.e., next vertex has been "marked" or there is no next
vertex)

DFS can be implemented efficiently using a


stack

Dr. Fares saab

22

Depth-First-Search (DFS)
(cont.)

Set found to false


stack.Push(startVertex)
DO
stack.Pop(vertex)
IF vertex == endVertex
Set found to true
ELSE
Push all adjacent vertices onto stack
WHILE !stack.IsEmpty() AND !found
IF(!found)
Write "Path does not exist"

Dr. Fares saab

23

start

end

(initialization)

Dr. Fares saab

24

Dr. Fares saab

25

Dr. Fares saab

26

template <class ItemType>


void DepthFirstSearch(GraphType<VertexType> graph, VertexType startVertex,
VertexType endVertex)
{

StackType<VertexType> stack;
QueType<VertexType> vertexQ;
bool found = false;
VertexType vertex;
VertexType item;
graph.ClearMarks();
stack.Push(startVertex);
do {
stack.Pop(vertex);
if(vertex == endVertex)
found = true;
Dr. Fares saab

(continues)
27

else {
if(!graph.IsMarked(vertex)) {
graph.MarkVertex(vertex);
graph.GetToVertices(vertex, vertexQ);
while(!vertexQ.IsEmpty()) {
vertexQ.Dequeue(item);
if(!graph.IsMarked(item))
stack.Push(item);
}
}
} while(!stack.IsEmpty() && !found);
if(!found)
cout << "Path not found" << endl;
}

Dr. Fares saab

(continues)
28

template<class VertexType>
void GraphType<VertexType>::GetToVertices(VertexType vertex,
QueTye<VertexType>& adjvertexQ)
{
int fromIndex;
int toIndex;
fromIndex = IndexIs(vertices, vertex);
for(toIndex = 0; toIndex < numVertices; toIndex++)
if(edges[fromIndex][toIndex] != NULL_EDGE)
adjvertexQ.Enqueue(vertices[toIndex]);
}
Dr. Fares saab

29

Breadth-First-Searching (BFS)

What is the idea behind BFS?

Look at all possible paths at the same depth before you go at a deeper
level

Back up as far as possible when you reach a "dead end" (i.e., next vertex
has been "marked" or there is no next vertex)

Dr. Fares saab

30

Breadth-First-Searching (BFS) (cont.)

BFS can be implemented efficiently using a


queue

IF(!found)
Write "Path does not exist"

Set found to false


queue.Enqueue(startVertex)
DO
queue.Dequeue(vertex)
IF vertex == endVertex
Set found to true
ELSE
Enqueue all adjacent vertices onto queue
WHILE !queue.IsEmpty() AND !found

Should we mark a vertex when it is enqueued or


when it is dequeued ?

Dr. Fares saab

31

start

end

(initialization)

Dr. Fares saab

32

next:
Dr. Fares saab

33

Dr. Fares saab

34

template<class VertexType>
void BreadthFirtsSearch(GraphType<VertexType> graph, VertexType
startVertex, VertexType endVertex);
{
QueType<VertexType> queue;
QueType<VertexType> vertexQ;//
bool found = false;
VertexType vertex;
VertexType item;
graph.ClearMarks();
queue.Enqueue(startVertex);
do {
queue.Dequeue(vertex);
if(vertex == endVertex)
found = true;
Dr. Fares saab

(continues)
35

else {
if(!graph.IsMarked(vertex)) {
graph.MarkVertex(vertex);
graph.GetToVertices(vertex, vertexQ);
while(!vertxQ.IsEmpty()) {
vertexQ.Dequeue(item);
if(!graph.IsMarked(item))
queue.Enqueue(item);
}
}
}
} while (!queue.IsEmpty() && !found);
if(!found)
cout << "Path not found" << endl;
}
Dr. Fares saab

36

Single-source shortest-path problem

There are multiple paths from a source vertex to a destination vertex

Shortest path: the path whose total weight (i.e., sum of edge weights)
is minimum

Examples:

Austin->Houston->Atlanta->Washington:

Austin->Dallas->Denver->Atlanta->Washington: 2980 miles

Dr. Fares saab

1560 miles

37

Single-source shortest-path problem


(cont.)

Common algorithms: Dijkstra's algorithm, Bellman-Ford algorithm

BFS can be used to solve the shortest graph problem when the graph
is weightless or all the weights are the same

(mark vertices before Enqueue)

Dr. Fares saab

38

You might also like