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

Graphs: CSE225: Data Structures and Algorithms

This document discusses graphs as a data structure. It defines graphs as consisting of vertices and edges that connect the vertices. It describes directed and undirected graphs, and how a graph can be represented as G = (V,E) where V is the set of vertices and E is the set of edges. It provides examples of complete graphs and weighted graphs. It also discusses how graphs can be implemented and stored in memory, including the use of an adjacency matrix. Functions for graph operations like adding vertices and edges are described.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Graphs: CSE225: Data Structures and Algorithms

This document discusses graphs as a data structure. It defines graphs as consisting of vertices and edges that connect the vertices. It describes directed and undirected graphs, and how a graph can be represented as G = (V,E) where V is the set of vertices and E is the set of edges. It provides examples of complete graphs and weighted graphs. It also discusses how graphs can be implemented and stored in memory, including the use of an adjacency matrix. Functions for graph operations like adding vertices and edges are described.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Lecture 15

Graphs
CSE225: Data Structures and Algorithms
Graphs
• Recap: The tree data structure
• Nodes
• Parent-child relation between two
nodes
Graphs
• Recap: The tree data structure
• Nodes
• Parent-child relation between two
nodes
• Remove the restriction that each node
may have only one parent node
• We have a graph a b

d e
Graphs
• Graph: A data structure that consists of a set of nodes and a set of connections
that relate the nodes to one another
• Vertex: A node in a graph
• Edge (arc): A pair of vertices representing a connection between two nodes in a
graph
• Undirected graph: A graph in which the edges have no direction
• Directed graph (digraph): A graph in which each edge is directed from one vertex
to another (or the same) vertex

a b a b
vertex

c edge c

d e d e
Undirected graph Directed graph
Graphs
• A graph G is represented as G = (V,E), where
V: set of vertices
E: set of edges connecting the vertices in V
• An edge e = (u,v) is a pair of vertices
• Example:

a b V= {a,b,c,d,e}

E= {(a,b),(a,c),(a,d),
c (b,e),(c,d),(c,e), (d,e)}

d e
Graphs
◦ Example: Graph1
Graphs
◦ Example: Graph2
Graphs
◦ Example: Graph3
Graphs
◦ Complete graph: A graph in which every vertex is directly connected to
every other vertex
Graphs
◦ Weighted graph: A graph in which each edge carries a value (cost)
Graphs
◦ Adjacent vertices: Two vertices in a graph that are connected by an edge
◦ Path: A sequence of vertices that connects two nodes in a graph
Implementation Issues
• How can we store a graph in memory?
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
0 Atlanta [0] 0 0 0 0 0 800 600
1 Austin [1] 0 0 0 200 0 160 0
2 Chicago [2] 0 0 0 0 1000 0 0
3 Dallas [3] 0 200 900 0 780 0 0
4 Denver [4] 1400 0 1000 0 0 0 0
5 Houston [5] 800 0 0 0 0 0 0
6 Washington [6] 600 0 0 1300 0 0 0
7
[7]
8
[8]
9
[9]

Vertices Edges
Implementation Issues
• How can we store a graph in memory?

Vertices

Edges
Graph Specification
Structure: The graph consists of a set of vertices and a set of weighted
edges that connect some or all of the vertices to one another.
Operations:
MakeEmpty
Function Initializes the graph to an empty state.
Postcondition Graph is empty.
Boolean IsEmpty
Function Tests whether the graph is empty.
Postcondition Returns true if graph is empty and false otherwise.
Boolean IsFull
Function Tests whether the graph is full.
Postcondition Returns true if graph is full and false otherwise.
Graph Specification
AddVertex(VertexType vertex)
Function Adds vertex to the graph.
Precondition Graph is not full.
Postcondition vertex is in V(graph).
AddEdge(VertexType fromVertex, VertexType toVertex, EdgeValueType weight)
Function Adds an edge with the specified weight from fromVertex to
toVertex.
Precondition fromVertex and toVertex are in V(graph).
Postcondition (fromVertex, toVertex) is in E(graph) with the specified weight.
Graph Specification
EdgeValueType WeightIs(VertexType fromVertex, VertexType toVertex)
Function Determines the weight of the edge from fromVertex to toVertex.
Precondition fromVertex and toVertex are in V(graph).
Postcondition Function value = weight of edge from fromVertex to toVertex, if
edge exists. If edge does not exist, function value = special "null-
edge" value.
GetToVertices(VertexType vertex, QueType& vertexQ)
Function Returns a queue of the vertices that are adjacent from vertex.
Precondition vertex is in V(graph).
Postcondition vertexQ contains the names of all vertices that are adjacent from
vertex.
graphtype.h
#include "QueType.h"

template<class VertexType> private:


class GraphType int numVertices;
{ int maxVertices;
public: VertexType* vertices;
GraphType(); int **edges;
GraphType(int maxV); bool* marks;
~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 IsMarked(VertexType);
graphtype.cpp
#include "GraphType.h"
template<class VertexType>
GraphType<VertexType>::GraphType()
{
numVertices = 0;
maxVertices = 50;
vertices = new VertexType[50];
edges = new int*[50];
for(int i=0;i<50;i++)
edges[i] = new int [50];
marks = new bool[50];
}
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];
}
graphtype.cpp
template<class VertexType>
GraphType<VertexType>::~GraphType()
{
delete [] vertices;
delete [] marks;
for(int i=0;i<maxV;i++)
delete [] edges[i];
delete [] edges;
}

const int NULL_EDGE = 0;


template<class VertexType>
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++;
}
graphtype.cpp
template<class VertexType>
int IndexIs(VertexType* vertices, VertexType vertex)
{
int index = 0;

while (!(vertex == vertices[index]))


index++;
return index;
}

template<class VertexType>
void GraphType<VertexType>::AddEdge(VertexType fromVertex,
VertexType toVertex, int weight)
{
int row = IndexIs(vertices, fromVertex);
int col= IndexIs(vertices, toVertex);
edges[row][col] = weight;
}
graphtype.cpp
template<class VertexType>
int GraphType<VertexType>::WeightIs(VertexType fromVertex, VertexType
toVertex)
{
int row = IndexIs(vertices, fromVertex);
int col= IndexIs(vertices, toVertex);

return edges[row][col];
}

template<class VertexType>
void GraphType<VertexType>::GetToVertices(VertexType vertex,
QueType<VertexType>& adjVertices)
{
int fromIndex;
int toIndex;

fromIndex = IndexIs(vertices, vertex);


for (toIndex = 0; toIndex < numVertices; toIndex++)
if (edges[fromIndex][toIndex] != NULL_EDGE)
adjVertices.Enqueue(vertices[toIndex]);
}

You might also like