Upload
meljun-cortes-mbampa
View
222
Download
0
Embed Size (px)
Citation preview
7/26/2019 MELJUN CORTES DATA STRUCTURES Graph
1/17
Data Structures
Graphs
one of the most versatile structures used in
computer programming
have shape dictated by a physical problem
nodesare called vertices(the singular is vertex)
Graphs * Property of STIPage 1 of 17
7/26/2019 MELJUN CORTES DATA STRUCTURES Graph
2/17
Data Structures
Graphs
Graphs * Property of STIPage 2 of 17
circles represent freeway interchanges andstraight lines connecting the circles representfreeways segments
the circles are the verticesand the lines are theedges
7/26/2019 MELJUN CORTES DATA STRUCTURES Graph
3/17
Data Structures
Adjacency
if they are connected by a single edge
Neighbors
Path
a sequence of edges Connected Graphs
Graphs
Graphs * Property of STIPage 3 of 17
7/26/2019 MELJUN CORTES DATA STRUCTURES Graph
4/17
Data Structures
Directed and Weighted Graphs
Non-directed graphs- edges dont have adirection
Directed graphs - model situations in which
can go in only one direction along an edge Weight - a number that can represent the
physical distance between two vertices
Graphs
Graphs * Property of STIPage 4 of 17
7/26/2019 MELJUN CORTES DATA STRUCTURES Graph
5/17
Data Structures
Vertices
Graphs
class Vertex
{
public char label;
public boolean wasVisited;
public Vertex(char lab)
{
Graphs * Property of STIPage 5 of 17
wasVisited = false;
}
}
7/26/2019 MELJUN CORTES DATA STRUCTURES Graph
6/17
Data Structures
Adjacency Matrix
two-dimensional array in which theelements indicate whether an edge is
present between two vertices
Graphs
Graphs * Property of STIPage 6 of 17
7/26/2019 MELJUN CORTES DATA STRUCTURES Graph
7/17
Data Structures
Adjacency List
refers to a linked list of a kind examined inthe discussion of recursion
Graphs
Graphs * Property of STIPage 7 of 17
7/26/2019 MELJUN CORTES DATA STRUCTURES Graph
8/17
Data Structures
Adding Vertices and Edges to a Graph
Creation of vertex
Inserting the edge
Graphs
vertexList[nVerts++] = new Vertex(F);
addjMat[1][3] = 1;
addjMat[3][1] = 1;
Graphs * Property of STIPage 8 of 17
7/26/2019 MELJUN CORTES DATA STRUCTURES Graph
9/17
Data Structures
Graphs
class Graph
{
private final int MAX_VERTS = 20;
private Vertex vertexList[];
private int adjMat[][];
private int nVerts;
public Graph()
{vertexList = new Vertex[MAX_VERTS];
adjMat = new int[MAX_VERTS][MAX_VERTS];
nVerts = 0;
for(int j=0; j
7/26/2019 MELJUN CORTES DATA STRUCTURES Graph
10/17
Data Structures
Depth-First Search
Searches
Graphs * Property of STIPage 10 of 17
Rule 1: If possible, visit an adjacent
unvisited vertex, mark it, and push it on the
stack. Rule 2: If you cant follow Rule 1, then, if
possible, pop vertex off the stack.
Rule 3: If you cant follow Rule 1 and Rule
2, its finished.
7/26/2019 MELJUN CORTES DATA STRUCTURES Graph
11/17
Data Structures
Searches
Depth-First Search
Graphs * Property of STIPage 11 of 17
public int getAdjUnvisitedVertex(int v)
{
for(int j=0; j
7/26/2019 MELJUN CORTES DATA STRUCTURES Graph
12/17
Data Structures
Depth-First Search
Searches
public void dfs()
{
vertexList[0].wasVisited = true;displayVertex(0);
theStack.push(0);
while( !theStack.isEmpty() )
{
Graphs * Property of STIPage 12 of 17
int v = getAdjUnvisitedVertex
( theStack.peek() );
if(v == -1)
theStack.pop();
else
{
vertexList[v].wasVisited = true;
displayVertex(v);
theStack.push(v);
}
}
for(int j=0; j
7/26/2019 MELJUN CORTES DATA STRUCTURES Graph
13/17
Data Structures
Depth-First Search creating a graph object
Searches
Graph theGraph = new Graph();
theGraph.addVertex('A');
theGraph.addVertex('B');
theGraph.addVertex('C');
theGraph.addVertex('D');
theGraph.addVertex('E');
theGraph.addEdge(0, 1);
theGraph.addEdge(1, 2);
Graphs * Property of STIPage 13 of 17
theGraph.addEdge(0, 3);
theGraph.addEdge(3, 4);
System.out.print("Visits: ");
theGraph.dfs();
System.out.println();
7/26/2019 MELJUN CORTES DATA STRUCTURES Graph
14/17
Data Structures
Breadth-First Search - stay as close aspossible to the starting point
Searches
Graphs * Property of STIPage 14 of 17
Rule 1: Visit the next unvisited vertex (if
there is one) thats adjacent to the current
vertex, mark it, and insert it into the queue.
Rule 2: If you cant carry out Rule 1
because there are no more unvisited
vertices, remove a vertex from the queue (if
possible) and make it the current vertex.
Rule 3: If you cant carry out Rule 2
because the queue is empty, its finished.
7/26/2019 MELJUN CORTES DATA STRUCTURES Graph
15/17
Data Structures
Breadth-First Search
Searches
Graphs * Property of STIPage 15 of 17
7/26/2019 MELJUN CORTES DATA STRUCTURES Graph
16/17
Data Structures
Searches
Breadth-First Search
public void bfs()
{
vertexList[0].wasVisited = true;
displayVertex(0);
theQueue.insert(0);
int v2;
while( !theQueue.isEmpty() )
Graphs * Property of STIPage 16 of 17
int v1 = theQueue.remove();
while((v2=getAdjUnvisitedVertex(v1)) != -1)
{
vertexList[v2].wasVisited = true;
displayVertex(v2);
theQueue.insert(v2);
}
}
for(int j=0; j
7/26/2019 MELJUN CORTES DATA STRUCTURES Graph
17/17
Data Structures
Minimum Spanning Trees
while( !theStack.isEmpty() )
{
Graphs * Property of STIPage 17 of 17
n curren er ex = e ac .pee ;
int v =getAdjUnvisitedVertex(currentVertex);
if(v == -1)
theStack.pop();else
{
vertexList[v].wasVisited = true;
theStack.push(v);
displayVertex(currentVertex);
displayVertex(v);
System.out.print(" ");}
}
for(int j=0; j