DOC PREVIEW
Penn CIT 594 - Graphs

This preview shows page 1-2-24-25 out of 25 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

GraphsGraph definitionsGraph terminology IGraph terminology IIGraph terminology IIIGraph terminology IVAdjacency-matrix representation IAdjacency-matrix representation IIEdge-set representation IEdge-set representation IIAdjacency-set representation IAdjacency-set representation IIAdjacency-set representation IIIAdjacency-set representation IVSearching a graphExample: Depth-first searchFinding connected componentsGraph applicationsShortest-pathDijkstra’s algorithm IDijkstra’s algorithm IIDijkstra’s algorithm IIISummaryA graph puzzleThe EndGraphs2Graph definitionsThere are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphsBirminghamRugbyLondonCambridgeBristolSouthhamptonDover60140190190150100120110An undirected graphstartfill panwith watertake eggfrom fridgebreak egginto panboilwateradd saltto waterA directed graph3Graph terminology IA graph is a collection of nodes (or vertices, singular is vertex) and edges (or arcs)Each node contains an elementEach edge connects two nodes together (or possibly the same node to itself) and may contain an edge attributeA directed graph is one in which the edges have a directionAn undirected graph is one in which the edges do not have a directionNote: Whether a graph is directed or undirected is a logical distinction—it describes how we think about the graphDepending on the implementation, we may or may not be able to follow a directed edge in the “backwards” direction4Graph terminology IIThe size of a graph is the number of nodes in itThe empty graph has size zero (no nodes)If two nodes are connected by an edge, they are neighbors (and the nodes are adjacent to each other)The degree of a node is the number of edges it hasFor directed graphs,If a directed edge goes from node S to node D, we call S the source and D the destination of the edgeThe edge is an out-edge of S and an in-edge of DS is a predecessor of D, and D is a successor of SThe in-degree of a node is the number of in-edges it hasThe out-degree of a node is the number of out-edges it has5Graph terminology IIIA path is a list of edges such that each node (but the last) is the predecessor of the next node in the listA cycle is a path whose first and last nodes are the sameExample: (London, Bristol, Birmingham, London, Dover) is a pathExample: (London, Bristol, Birmingham, London) is a cycleA cyclic graph contains at least one cycleAn acyclic graph does not contain any cyclesBirminghamRugbyLondonCambridgeBristolSouthhamptonDover601401901901501001201106Graph terminology IVAn undirected graph is connected if there is a path from every node to every other nodeA directed graph is strongly connected if there is a path from every node to every other nodeA directed graph is weakly connected if the underlying undirected graph is connectedNode X is reachable from node Y if there is a path from Y to X A subset of the nodes of the graph is a connected component (or just a component) if there is a path from every node in the subset to every other node in the subset7Adjacency-matrix representation IOne simple way of representing a graph is the adjacency matrixA 2-D array has a mark at [i][j] if there is an edge from node i to node jThe adjacency matrix is symmetric about the main diagonalABGEFDCABCDEFGA B C D E F G• This representation is only suitable for small graphs! (Why?)8Adjacency-matrix representation IIAn adjacency matrix can equally well be used for digraphs (directed graphs)A 2-D array has a mark at [i][j] if there is an edge from node i to node jAgain, this is only suitable for small graphs!ABGEFDCABCDEFGA B C D E F G9Edge-set representation IAn edge-set representation uses a set of nodes and a set of edgesThe sets might be represented by, say, linked listsThe set links are stored in the nodes and edges themselvesThe only other information in a node is its element (that is, its value)—it does not hold information about its edgesThe only other information in an edge is its source and destination (and attribute, if any)If the graph is undirected, we keep links to both nodes, but don’t distinguish between source and destinationThis representation makes it easy to find nodes from an edge, but you must search to find an edge from a nodeThis is seldom a good representation10Edge-set representation IIHere we have a set of nodes, and each node contains only its element (not shown)ABGEFDCpqrstuvwEach edge contains references to its source and its destination (and its attribute, if any)edgeSet = { p: (A, E), q: (B, C), r: (E, B), s: (D, D), t: (D, A), u: (E, G), v: (F, E), w: (G, D) }nodeSet = {A, B, C, D, E, F, G}11Adjacency-set representation IAn adjacency-set representation uses a set of nodesEach node contains a reference to the set of its edgesFor a directed graph, a node might only know about (have references to) its out-edgesThus, there is not one single edge set, but rather a separate edge set for each nodeEach edge would contain its attribute (if any) and its destination (and possibly its source)12Adjacency-set representation IIHere we have a set of nodes, and each node refers to a set of edgesABGEFDCpqrstuvwA { p }B { q }C { }D { s, t }E { r, u }F { v }G { w }Each edge contains references to its source and its destination (and its attribute, if any) p: (A, E)q: (B, C)r: (E, B)s: (D, D)t: (D, A)u: (E, G)v: (F, E)w: (G, D)13Adjacency-set representation IIIIf the edges have no associated attribute, there is no need for a separate Edge classInstead, each node can refer to a set of its neighborsIn this representation, the edges would be implicit in the connections between nodes, not a separate data structureFor an undirected graph, the node would have references to all the nodes adjacent to itFor a directed graph, the node might have:references to all the nodes adjacent to it, orreferences to only those adjacent nodes connected by an out-edge from this node14Adjacency-set representation IVHere we have a set of nodes, and each node refers to a set of other (pointed to) nodesThe edges are implicitABGEFDCA { E }B { C }C { }D { D, A }E { B, G }F { E }G { D }15Searching a graphWith certain modifications, any tree search technique can be applied to a graphThis


View Full Document

Penn CIT 594 - Graphs

Documents in this Course
Trees

Trees

17 pages

Searching

Searching

24 pages

Pruning

Pruning

11 pages

Arrays

Arrays

17 pages

Stacks

Stacks

30 pages

Recursion

Recursion

25 pages

Hashing

Hashing

24 pages

Recursion

Recursion

24 pages

Storage

Storage

37 pages

Trees

Trees

21 pages

Arrays

Arrays

24 pages

Hashing

Hashing

24 pages

Recursion

Recursion

25 pages

Graphs

Graphs

23 pages

Graphs

Graphs

25 pages

Stacks

Stacks

25 pages

Recursion

Recursion

25 pages

Quicksort

Quicksort

21 pages

Quicksort

Quicksort

21 pages

Graphs

Graphs

25 pages

Recursion

Recursion

25 pages

Searching

Searching

24 pages

Counting

Counting

20 pages

HTML

HTML

18 pages

Recursion

Recursion

24 pages

Pruning

Pruning

11 pages

Graphs

Graphs

25 pages

Load more
Download Graphs
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Graphs and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Graphs 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?