一个简单图搜索算法

[Serializable]      public class VPoint    {        public int ID { get; set; }        public int X { get; set; }        public int Y { get; set; }        public bool IsFind { get; set; }        public List<int> Dependences { get; set; }//与该点关联的点ID

    }

private void FindPath()
        {
            VPoint v1 = new VPoint() { ID = 0, X = 0, Y = 20, Dependences = new List<int>() {2, 1 } };
            VPoint v2 = new VPoint() { ID = 1, X = 20, Y = 0, Dependences = new List<int>() { 0, 3, 2 } };
            VPoint v3 = new VPoint() { ID = 2, X = 20, Y = 20, Dependences = new List<int>() { 0, 1, 4 } };
            VPoint v4 = new VPoint() { ID = 3, X = 40, Y = 0, Dependences = new List<int>() { 1, 4, 5 } };
            VPoint v5 = new VPoint() { ID = 4, X = 40, Y = 20, Dependences = new List<int>() { 2, 3, 5 } };
            VPoint v6 = new VPoint() { ID = 5, X = 60, Y = 20, Dependences = new List<int>() { 3, 4 } };
            List<VPoint> points = new List<VPoint>() { v1, v2, v3, v4, v5, v6 };
            VPoint start = v1;
            VPoint end = v6;
            List<VPoint> resultpoint = new List<VPoint>();
            FindPoint(Clone(points), start, end, resultpoint);
            foreach (var item in Paths)
            {
                string path = "";
                foreach (var point in item)
                {
                    path += point.ID +"=>";
                }
                Console.WriteLine(path);//打印路径
            }
            Console.WriteLine(count);
        }
        int count = 0;
        List<IList<VPoint>> Paths = new List<IList<VPoint>>();//全局变量,保存所有的路径
        private void FindPoint(List<VPoint> points, VPoint start, VPoint end, List<VPoint> resultpoint)
        {
            resultpoint.Add(start);
            int index = points.IndexOf(points.Where(p => p.ID == start.ID).ToList()[0]);
            points[index].IsFind = true; //标记已经查找过
            if (start.Dependences.Contains(end.ID))//如果是终点
            {
                resultpoint.Add(end);
                Paths.Add(resultpoint);
                int _index = points.IndexOf(points.Where(p => p.ID == end.ID).ToList()[0]);
                points[_index].IsFind = true;
            }
            else
            {
                foreach (var item in start.Dependences)
                {
                    var newstart = points.Where(p => p.ID == item && p.IsFind == false).ToList();
                    if (newstart.Count > 0)
                    {
                        count++;
                        List<VPoint> newpoints = Clone(resultpoint);
                        FindPoint(Clone(points), newstart[0], end, newpoints);
                    }
                }
            }
        }
        public static T Clone<T>(T RealObject)//深度拷贝
        {
            using (Stream objectStream = new MemoryStream())
            {
                IFormatter formatter = new BinaryFormatter();
                formatter.Serialize(objectStream, RealObject);
                objectStream.Seek(0, SeekOrigin.Begin);
                return (T)formatter.Deserialize(objectStream);
            }
        }  
时间: 2024-08-29 13:57:11

一个简单图搜索算法的相关文章

GEF - 制作一个简单图形化编辑框架笔记1

在首先来看看GEF是什么,GEF的全称是Graphical Editing Framework,图形化框架,可以利用此框架做图形化编.他的基本原理是采用MVC开发模式. 以下是一些例子 GEF里面包含有, EditorPart - 编辑的容器,所有的图形都可以规则都存放在这个容器当中 EditPart - 图形的C部分,控制图形的内容与展示的重要中介 Figure - 图形的V部分,关于图形的形状部分,都会放在这里 Model - 图形的M部分,记录图形的属性 EditorPart 这部分是容器

对于宫格地图寻最短路径的一个广度搜索算法

1 import java.util.ArrayDeque; 2 import java.util.ArrayList; 3 import java.util.HashMap; 4 import java.util.Iterator; 5 import java.util.Map.Entry; 6 import java.util.Queue; 7 8 public class TestPath { 9 10 public static void main(String[] args) { 11

关于Havel算法判断度数序列能否构成简单图的思考

问题描述: Given a list of n natural numbers d1, d2,...,dn, show how to decide in polynomial time whether there exists an undirected graph G = (V, E) whose node degrees are precisely the numbers d1,d2,···,dn. G should not contain multiple edges between th

hdu 2454 Degree Sequence of Graph G (判断简单图)

///已知各点的度,判断是否为一个简单图 #include<stdio.h> #include<algorithm> #include<string.h> using namespace std; int a[1010]; bool cmp(int x,int y) { return x>y; } int main() { int t,n,i,j; scanf("%d",&t); while(t--) { int flag=1; sca

HDU 2454 Degree Sequence of Graph G(Havel定理 判断简单图的存在)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2454 Problem Description Wang Haiyang is a strong and optimistic Chinese youngster. Although born and brought up in the northern inland city Harbin, he has deep love and yearns for the boundless oceans.

hdu 2454 Degree Sequence of Graph G (推断简单图)

///已知各点的度,推断是否为一个简单图 #include<stdio.h> #include<algorithm> #include<string.h> using namespace std; int a[1010]; bool cmp(int x,int y) { return x>y; } int main() { int t,n,i,j; scanf("%d",&t); while(t--) { int flag=1; sca

初识禁忌搜索算法

一周前和实验室师弟一起探讨的,在我的影响下他开始去坐毕设了...啧啧:现在等我同学过来找我,把那次的讨论内容回忆一下. 写一写个人理解,语句比较混乱,只一个入门,我并没有深入研究过. 这是一个启发式搜索算法. 以解决TSP问题为例,假设ABCDE五个城市,各个城市间距离的无向图. 1.假设以A开头,ABCDE,这个假设是随意的,因为TSP是环,没有开头,计算距离的时候需要加上环. 2.BCDE两两交换,注意是相邻交换,不是A(n,2),是BC,CD,DE交换,算出所有的TSP距离,注意ABCDE

[一些基础算法的小心得] -- 二分搜索算法

对分搜索算分也叫二分搜索算法也叫,英文则是binary-search  algorithm.其概念非常的基础,这里不再描述.但问题是我们能否不加思考的写出一个二分搜索算法并一次运行成功呢? 我们知道其核心部分的伪码非常简单(短): 并且我们也知道,对于一个规模为n的已排序数组,任何基于比较的搜索算分所需最坏情况时间为O(n). 那么下面这种算法是否正确呢?如果正确的话,最坏情况时间是什么? 那么下面这种算法呢? 以上三种写法,你能区分出哪种是正确的哪种是不正确的吗,不正确的部分是哪里如何修改呢.

关于图论的若干巴拉巴拉

最近课堂上正在讲图论 先安利MIT课程:http://open.163.com/special/opencourse/algorithms.html 因为本人对图论的概念并不是很清楚,所以还是整理一下吧. 1.图论的基本概念 几种常见的图的分类: 类型 边 允许多重边 允许环 简单图 无向 否 否 多重图 无向 是 否 伪图 无向 是 是 有向图 有向 否 是 有向多重图 有向 是 是 完全图:n个顶点上的完全图是在每对不同顶点之间都恰有一条边的简单图. 二分图:若把简单图G的顶点集合分为两个不