Graphs

Chapter 29 in Carrano/Prichard

 

Graph topics

•      Definitions and terminology

–    Types of graphs

•      Some examples

•      Traversals

–    Breadth-first

–    Depth-first

•      Topological Order

•      Representing graphs in the computer

–    Adjacency Matrix

–    Adjacency List

•      Graph Algorithms

–    Shortest path

 

Graph definitions

•      A graph G = (V, E) consists of set of vertices, V and a set of edges, E.

•      Each edge is a pair (v, w) where v, w Є V

•      Three types of graphs are determined by the edges

–    Simple graph – no multiple edges between vertices

–    Multigraph – can have multiple edges, but no loops

–    Pseudograph – a multigraph with loops allowed

 

 

•      If the pair of edges is ordered, then the graph is directed, sometimes called a digraph.

–    i.e. (v, w) may be different than (w, v)

•      A path is a sequence of edges connecting vertices

•      A cycle is a path in which the first and last vertices are the same and there are no repeated edges

 

More terminology

•      The edges in either a graph or a diagraph can be weighted.

•      Two  vertices are adjacent if an edge exists between them;

–    the edge is incident with its vertices

•      The degree of a vertex in an undirected graph is the number of edges incident with it

–    A loop contributes twice because it goes in and comes out; also

•    An isolated vertex has degree zero

•    A pendant vertex has degree one

•      Handshaking Theorem:  the sum of the degrees of the vertices is twice the number of edges

•      Theorem: An undirected graph has an even number of vertices of odd degree.

 

Vertices in directed graphs

•      When (u,v) is an edge of the directed graph

–   the vertex u is called the initial vertex of (u,v) and

–   v is called the terminal or end vertex of (u,v)

•      The in-degree of a vertex v is the number of edges with v as their terminal vertex 

•      The out-degree of a vertex v is the number of edges with v as their initial vertex

•      A loop at a vertex is both an in-degree and out-degree

 

 

Examples

On the handout, for graphs 1-3 find:

–         the number of vertices and the number of edges

–         the degree of each vertex (put on the graph)

–         the sum of the degrees of the vertices

For the graphs 7-9

5. Find the in-degree and out-degree of each vertex (put on the graph)

6. Determine the sum of the in-degrees of the vertices and

7. the sum of the out-degrees

 

A weighted directed graph

•      Naming paths—in a directed graph, a path is a sequence of vertices such that there is an edge from each vertex to its successor

–      Name a path from v1 to v3

–    Name another

•      The length of a path is the sum of the weights on the path

–    What is the length of the paths named above?

–    What is the weight of the path from v5 to v4?

•      A cycle is a path from a vertex to itself

–    Name a cycle in the graph on the overhead.

•      A path is simple if it does not contain a cycle.

 

Some special simple graphs

•      A complete graph is a has exactly one edge between each pair of distinct vertices 

–    Denoted Kn

–    Find a formula to determine how many edges a complete graph has, given the number of vertices.

•      Cycles consist of n vertices (n>2) where the vertices are connected in a cycle

–    It has the same number of edges as vertices

•      Wheels have one more vertex than a  cycle; the new vertex connects to all the other vertices

–    Wheels have twice as many edges as vertices

 

Traversals

•      Usually you search a tree for a node that contains a particular value

–    Preorder, inorder and postorder traversals are examples of depth-first searches

–    In this type of search, you go as deep in the tree until you reach a leaf

•      Graphs focus on the connections between vertices, rather than the contents of a vertex

–    A breadth-first search can also be used with graphs

–    In this type of search, all the nodes at one level are visited before going deeper into the graph

•      Visiting nodes

–    In a tree, when a node is “visited” we mean to process the data at that node

–    In a graph, when a node is “visited”, we mean simply to “mark the node as visited”

 

 

Breadth-first  & depth-first traversals

•      Given an origin vertex, a breadth-first traversal visits the origin and the all the origin’s adjacent vertices (called neighbors),

–    Then it considers each of these vertices neighbors

–    The vertices closest to the start are evaluated first

–    The most distant are evaluated last.

•      Given an origin vertex, a depth-first traversal visits one adjacent node, then keeps visiting nodes that are adjacent

–    When it can go no further, it backtracks until it comes to an adjacent node that has not been visited

–    A depth-first search is also called backtracking

•      For any graph, either a depth first traversal, or a breadth first traversal can be done

 

Representation of graphs

•      There are two main ways to represent graphs in the computer

–     Adjacency matrix

–     Adjacency list

Adjacency matrix

–    Use a 2-d array

–    For each edge (u, w) set A[u][v] to true, otherwise it is set to false

–    If the graph is weighted, set A[u][v] equal to the weight; use a sentinel for no edge

–    This method is simple, but uses a lot of space

•    The space requirement is Θ(|V2|)

–    but not very good if the graph is non dense--sparse

•    A dense graph has V2 edges

Adjacency list

•      If the graph is sparse, an adjacency list is a better representation

•      For each vertex, we keep list of all adjacent vertices

–    Keep an array of pointers, with all the nodes adjacent to a vertex in a linked list

–    If the edges have weights, this information is also stored in the cells

–    If an undirected graph, each edge appears in two lists

–    The space requirement is Θ(|V| + |E|)

•      This is the standard was to represent graphs

 

Using adjacency lists

•      Information about each vertex is stored in an object of type Vertex

–    This includes the list of its adjacent vertices

–    In most real-life applications, vertices have names, which are unknown at compile-time

•    Usually a hash table maps the names to their corresponding Vertex object

•    Each vertex object keeps a list of the vertices adjacent to it

–    New vertex objects are created as the graph is read, and the edges are put into its list

–    If the vertex of the new edge has not yet been created, it is created at this time and put into the hash table