Chapter 9 Data Structure Slide
Chapter 9 Data Structure Slide
Data Structures
What is a graph?
A data
The
Trees vs graphs
Graph terminology
is adjacent
7
Path: a sequence of vertices that connect 5two
nodes in atograph
7 is adjacent from 5
O( N )
Graph implementation
Array-based implementation
10
Array-based implementation
11
Linked-list implementation
12
Linked-list implementation
13
Adjacency matrix
Adjacency list
14
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
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];
}
18
Graphs as ADTs
ADT graph operations
Insert vertex in graph whose vertices have distinct values that differ from new
vertexs value.
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, 2013
Graphs as ADTs
ADT graph operations, ctd.
Remove specified vertex from graph and any edges between the vertex and other
vertices.
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)
21
Depth-First-Search (DFS)
22
Depth-First-Search (DFS)
(cont.)
23
start
end
(initialization)
24
25
26
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;
}
(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)
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)
30
IF(!found)
Write "Path does not exist"
31
start
end
(initialization)
32
next:
Dr. Fares saab
33
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
Shortest path: the path whose total weight (i.e., sum of edge weights)
is minimum
Examples:
Austin->Houston->Atlanta->Washington:
1560 miles
37
BFS can be used to solve the shortest graph problem when the graph
is weightless or all the weights are the same
38