Upload
clinton-jordan
View
218
Download
1
Embed Size (px)
Citation preview
GraphsGraphs
DefinitionsDefinitions
A graph is two sets.A graph is two sets.– A set of nodes or vertices A set of nodes or vertices VV– A set of edges A set of edges EE
Edges connect nodes.Edges connect nodes. The number of vertices |The number of vertices |VV|| The number of edges |The number of edges |EE|| A A sparsesparse graph is one with relatively few graph is one with relatively few
edgesedges A A densedense graph is one with relatively many graph is one with relatively many
edgesedges A graph with all possible edges is A graph with all possible edges is completecomplete
More DefinitionsMore Definitions
A graph with edges directed from one vertex A graph with edges directed from one vertex to another is a to another is a direct graphdirect graph or a or a diagraphdiagraph
A graph whose edges are not directed is A graph whose edges are not directed is called an called an undirected graphundirected graph
A graph with labels associated with its A graph with labels associated with its vertices is called a vertices is called a labeled graphlabeled graph
Two vertices that share an edge are called Two vertices that share an edge are called adjacentadjacent. They are also called . They are also called neighbors.neighbors.
An edge connecting vertices u and v is An edge connecting vertices u and v is written (u,v). The edge is said to be incident written (u,v). The edge is said to be incident on u and v.on u and v.
Associated with each edge may be a cost or Associated with each edge may be a cost or a a weightweight..
Still More DefinitionsStill More Definitions
A sequence of vertices vA sequence of vertices v11, v, v22,…,v,…,vnn forms forms a a pathpath of length n-1 if there exist edges of length n-1 if there exist edges from vfrom vii to v to vi+1i+1 for 1 <= i < n. for 1 <= i < n.
A path is A path is simplesimple if all vertices on the if all vertices on the path are uniquepath are unique
A A cyclecycle is a path of length 3 or more is a path of length 3 or more that connects some vertex to itself.that connects some vertex to itself.
A cycle is simple if the path is simple A cycle is simple if the path is simple except for the first and last vertices.except for the first and last vertices.
Yes More DefinitionsYes More Definitions
A A subgraph Ssubgraph S is formed from is formed from Graph GGraph G by selecting a subset of by selecting a subset of VVss of of G’sG’s vertices and a subset vertices and a subset EEss of of G’sG’s edges edges such that for every edge such that for every edge EE in in EEss, both , both whose vertices are in whose vertices are in VVss..
An undirected graph is connected if An undirected graph is connected if there is at least one path from any there is at least one path from any vertex to any other.vertex to any other.
A graph without cycles is called A graph without cycles is called acyclicacyclic.. A directed graph without cycles is called A directed graph without cycles is called
directed acyclic graph directed acyclic graph or or DAGDAG
Graph RepresentationsGraph Representations
Adjacency MatrixAdjacency Matrix– If a pair of vertices have an edge If a pair of vertices have an edge
between them, then there is a 1 in between them, then there is a 1 in the matrix.the matrix.
Adjacency ListAdjacency List– If a vertex has an edge, then a node If a vertex has an edge, then a node
is added to its list with the other is added to its list with the other node as the datanode as the data
Graph Graph ImplementationsImplementations A common activity that graphs must A common activity that graphs must
support is traversalssupport is traversals This usually requires finding the node This usually requires finding the node
that is closest or the first nodethat is closest or the first node Then you usually need to find the next Then you usually need to find the next
node after some given node.node after some given node. Another common member needed for Another common member needed for
a graph implementation is a way to a graph implementation is a way to mark each of the verticesmark each of the vertices
Graph TraversalsGraph Traversals
To visit the vertices of a graph in To visit the vertices of a graph in some specific order based on the some specific order based on the graph’s topology.graph’s topology.
There are some troublesome issuesThere are some troublesome issues– It may not be possible to reach all of It may not be possible to reach all of
the vertices from each otherthe vertices from each other– The graph may contain cycles and we The graph may contain cycles and we
need to make sure that the cycles do need to make sure that the cycles do not cause an infinite loopnot cause an infinite loop
The MarkThe Mark
Graphs will typically maintain a Graphs will typically maintain a mark for each vertex in the mark for each vertex in the graph.graph.
They will clear the marks before a They will clear the marks before a traversal begins and set the mark traversal begins and set the mark as they go.as they go.
When the traversal is done we When the traversal is done we can check the marks to see if all can check the marks to see if all the vertices have been visited.the vertices have been visited.
Depth-first SearchDepth-first Search
Whenever a vertex is visited, a DFS will recursively Whenever a vertex is visited, a DFS will recursively visit all of its unvisited neighbors.visit all of its unvisited neighbors.
void DFS(Graph* G, int v) void DFS(Graph* G, int v) {{
Previsit(G, v);Previsit(G, v);G->setMark(v, VISITED);G->setMark(v, VISITED);for(int w=G->first(v); w<G->n(); w=G->next(v, for(int w=G->first(v); w<G->n(); w=G->next(v, w) )w) )
if (G->getMark(v) == UNVISITED)if (G->getMark(v) == UNVISITED)DFS(G, w);DFS(G, w);
PostVisit(G,v);PostVisit(G,v);}}
Breadth-First SearchBreadth-First Search
Examines all vertices connected Examines all vertices connected to the start vertex before visiting to the start vertex before visiting vertices further awayvertices further away
Similar to DFS except a queue Similar to DFS except a queue replaces the recursion stack.replaces the recursion stack.
If the graph is a tree, this is If the graph is a tree, this is equivalent to traversing level by equivalent to traversing level by level.level.
Shortest Paths Shortest Paths ProblemProblem Single-source shortest-pathSingle-source shortest-path
– Given a vertex v in a graph G, what Given a vertex v in a graph G, what is the shortest path from v to all is the shortest path from v to all other vertices in Gother vertices in G
This is usually solved by a classic This is usually solved by a classic algorithm.algorithm.
Dijkstra’s AlgorithmDijkstra’s Algorithm
Dijkstra’s AlgorithmDijkstra’s Algorithm
The algorithm maintains a distance The algorithm maintains a distance estimate from each vertex to every other estimate from each vertex to every other vertex.vertex.
Initially, every distance is set to infinity.Initially, every distance is set to infinity. Vertices are process in order of distance.Vertices are process in order of distance. Whenever a vertex is processed, its Whenever a vertex is processed, its
distance is updated for all of its distance is updated for all of its neighbors.neighbors.
Minimum-Cost Minimum-Cost Spanning TreesSpanning Trees MST problem takes a connected, MST problem takes a connected,
undirected, weighted graph.undirected, weighted graph. The MST is the graph containing The MST is the graph containing
the vertices of G along with the the vertices of G along with the subset of G’s edges that subset of G’s edges that – Has minimum total cost as Has minimum total cost as
measured by summing the values measured by summing the values for all of the edges in the subsetfor all of the edges in the subset
– Keeps the vertices connected.Keeps the vertices connected.
Minimum-Cost Minimum-Cost Spanning TreeSpanning Tree There are two main algorithms for There are two main algorithms for
solving this problemsolving this problem– Prim’s algorithmPrim’s algorithm– Kruskal’s algorithmKruskal’s algorithm